Вопрос

I'm testing that email is not required on the user model on update.

With FactoryGirl:

u = FactoryGirl.create(:user)
u.email = nil
expect(u.save).to be_true 

The test passes.

With shoulda:

should_not validate_presence_of(:email).on(:update)

The test fails with error:

Failure/Error: should_not validate_presence_of(:email)
Did not expect errors to include "can't be blank" when email is set to nil, got error:     can't be blank

Anyone have any thoughts on why this discrepancy occurs?

Это было полезно?

Решение

I think this is what's happening.

The should_not test is using an implicit subject of User.new. In order to test update on this subject, it must first be saved, but saving will result in an error because of the validation on create. The workaround is create/save a valid user object and to call should_not validate_presence_of(...).on(:update) explicitly on that object.

See the related Shoulda: Test validates_presence_of :on => :update

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top