2012-05-23 230 views
0

我正在学习Rspec。当我运行我的测试用例进行电子邮件验证时,我收到了一个无法理解的错误。请解释一下。rspec电子邮件验证

1) User should check for email validation 
    Failure/Error: @test_user.should have(1).errors_on(:email) 
     expected 1 errors on :email, got 2 
    # ./spec/models/user_spec.rb:17:in `block (2 levels) in <top (required)>' 

Finished in 0.12903 seconds 
2 examples, 1 failure 

我的测试用例是下面:

it"should check whether name is present or not" do 
    @test_user.name = nil 
    @test_user.should have(1).errors_on(:name) 
    end 

    it "should check for email validation"do 
    @test_user.email = nil 
    @test_user.should have(2).errors_on(:email) 
    end 
end 
+0

我只是重读你的问题,我显然没有读过它不够彻底。我可能猜测你的@test_user的状态在测试之间没有得到重置。你能否提供你的spec文件中设置这个变量的部分? – dogenpunk

回答

0

如果你正在验证既存在验证和某种形式验证的电子邮件属性,那么你会通过设置让两个错误属性为零。

也许你可以试试:

validates :email, presence: true, 
        format: { with: %r\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/, 
          allow_nil: true } 
+0

是的! @dogenpunk非常感谢! :) –