Kranthi
Kranthi

Reputation: 1417

rspec email validation

I'm learning Rspec. When i run my test case for email validation I'm getting an error which am not able to understand.Please explain.

  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

My test cases are below:

  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

Upvotes: 0

Views: 1828

Answers (1)

dogenpunk
dogenpunk

Reputation: 4382

If you're validating the email attribute with both the presence validator and some sort of format validator, then you'd get two errors by setting the attribute to nil.

Maybe try something like:

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

Upvotes: 1

Related Questions