如何使用rspec测试检查rails full app

时间:2015-05-07 13:08:22

标签: ruby-on-rails testing rspec rspec-rails

工厂数据也定义

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')

1 个答案:

答案 0 :(得分:0)

不知何故,你正在重复ids。

如果你在工厂设置id,只需删除它,数据库应该处理它。

另一种解决方案可能是重新创建测试数据库,但可能会再次出现问题。

如果在运行测试时进行调试,请不要使用quit,或者将大量垃圾保留在数据库中,而是使用continue并让事务回滚。< / p>