ruby-on-rails-3 – Rails 3 Rspec 2:验证失败:已经发送了电子
发布时间:2020-12-16 19:23:15 所属栏目:百科 来源:网络整理
导读:我有2个型号,User和Bucket.用户has_many Buckets和Bucket belongs_to a User. 在factories.rb,我有: Factory.define :user do |user| user.email "teste@test.com" user.password "foobar" user.password_confirmation "foobar"endFactory.sequence :email
我有2个型号,User和Bucket.用户has_many Buckets和Bucket belongs_to a User.
在factories.rb,我有: Factory.define :user do |user| user.email "teste@test.com" user.password "foobar" user.password_confirmation "foobar" end Factory.sequence :email do |n| "person-#{n}@example.com" end Factory.define :bucket do |bucket| bucket.email "user@example.com" bucket.confirmation false bucket.association :user end 我有一个login_user模块,如下所示: def login_user before(:each) do @request.env["devise.mapping"] = Devise.mappings[:user] @user = Factory.create(:user) #@user.confirm! sign_in @user end end 我正在使用Spork和Watch,我的Buckets_controller_spec.rb非常简单: describe "User authenticated: " do login_user @bucket = Factory(:bucket) it "should get index" do get 'index' response.should be_success end ... end 错误总是一样的: Failures: 1) BucketsController User authenticated: should get index Failure/Error: Unable to find matching line from backtrace ActiveRecord::RecordInvalid: Validation failed: Email has already been taken # ./lib/controller_macros.rb:12:in `block in login_user' 它只发生在我有工厂(:桶)时.当我不添加Factory(:bucket)时,登录正常. 它总是一样的错误.我尝试过添加:email => Factory.next(:email)给用户,但没有成功. 编辑: 在rails c测试中: ruby-1.9.2-p180 :019 > bucket = Factory(:bucket,:email => "hello@hello.com") ActiveRecord::RecordInvalid: Validation failed: Email has already been taken ruby-1.9.2-p180 :018 > Bucket.create(:email => "hello@hello.com") => #<Bucket id: 2,email: "hello@hello.com",confirmation: nil,created_at: "2011-04-08 21:59:12",updated_at: "2011-04-08 21:59:12",user_id: nil> 编辑2: 我发现错误在关联中,但是,我不知道如何解决它. bucket.association :user 解决方法
定义具有关联的工厂时,无论何时使用工厂,都需要向工厂提供要关联的对象.
这应该工作: describe "User authenticated: " do login_user @bucket = Factory(:bucket,:user => @user) it "should get index" do get 'index' response.should be_success end end 这样,女工就知道制作一个与@user相关的桶. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |