如何使用 rspec 测试检查 rails 完整应用

how to check rails full app using rspec test

工厂数据也定义

我的代码在controller_spec.rb

describe "POST create" do
  context "with valid attributes" do
    it "creates a new corporate" do
      post :create, corporate: FactoryGirl.attributes_for(:corporate)
      expect(Corporate.count).to eq(1)
    end
    it "redirects to the new corporate" do
      post :create, corporate: FactoryGirl.attributes_for(:corporate)
      response.should redirect_to Corporate.last
    end
  end
end

Error
 CorporateController POST create with valid attributes redirects to the new corporate
     Failure/Error: post :create, corporate: FactoryGirl.attributes_for(:corporate)
     ActiveRecord::RecordNotUnique:
       Mysql2::Error: Duplicate entry '7' for key 'PRIMARY': INSERT INTO `corporates` (`id`, `name`, `numberofemployees`, `address`, `phone`, `pending_moderation`, `created_at`, `updated_at`) VALUES (7, 'sumittest', 5, 'lkjgkklgslkjlk', '99999999999', 1, '2015-05-07 12:56:59.816181', '2015-05-07 12:56:59.816181')

不知何故你在重复 ID。

如果你是在工厂设置id,直接删掉,数据库应该会处理。

另一种解决方案可能是重新创建您的测试数据库,但您可能会再次遇到该问题。

如果在 运行 测试期间进行调试,请不要使用 quit 退出,否则数据库中会保留大量垃圾,而应使用 continue 并让要回滚的事务。