bcardarella / valid_attribute

Minimalist validation BDD for ActiveModel specs
178 stars 18 forks source link

validates_uniqueness_of error #2

Closed andyl closed 13 years ago

andyl commented 13 years ago

When I add 'validates_uniqueness_of' to my model, all of my valid_attribute specs fails with error message "undefined method: 'text?' for nil:NilClass".

Is there a workaround ??

Tx, Andy

bcardarella commented 13 years ago

Can you gist your model and your spec?

andyl commented 13 years ago

Here's the model: https://gist.github.com/967624 Here's the spec: https://gist.github.com/967629

bcardarella commented 13 years ago

On which attribute are you adding the uniqueness validator?

Also, check out my form of the gist: https://gist.github.com/967720

This is a more concise way of writing the validation specs. You can pass more than one value to the when method

andyl commented 13 years ago

I'd like to set the uniqueness validation on the ':login' field. In the gist, it's commented out. When I uncomment that validation, I get the error.

Re: multiple tests on a single line - yeah I have used that - in the gist I published I used one line per test so errors would point directly to the failing case.

bcardarella commented 13 years ago

So this is not an issue with valid_attribute. the login attribute is an accessor method on your model. validates_uniqueness_of tries to query the users table for a login column but that column doesn't exist. That is what is producing your error.

bcardarella commented 13 years ago

As for the reason why this was causing all of your specs to fail it is because valid_attribute called #valid? on the model as a whole. So it doesn't matter if you're referring to a single attribute or not the entire model is validated. This is probably not ideal, I should switch it to only validate the given attribute.

bcardarella commented 13 years ago

Actually, I take that back. I'll leave it as is. Otherwise I would be tying the implementation too tightly to ActiveModel.