[rspec-users] Test::Unit Functional failure puzzle
Matt Wynne
matt at mattwynne.net
Sun Nov 23 07:54:43 EST 2008
On 21 Nov 2008, at 14:44, James Byrne wrote:
> David Chelimsky wrote:
>
>>
>> Without seeing the controller code, I'd guess that the controller
>> uses
>> create, and not create! and that a validation failure is not getting
>> reported anywhere.
>>
>> If so, try changing create to create! and you should get your error.
>>
>
> Thank you. You were close enough to the actual situation that I could
> easily see what had to be done.
>
> The main problem (other than my abysmal ignorance) is that I am
> retro-fitting test to code that I wrote some time ago. In this case
> client is a role associated to an entity. The clients_controller
> handles the case where the entity and the client are created
> together in
> one pass. I was creating a new client but not a new entity in my test
> case and so the save was failing because the entity validations were
> failing. Changing the .save to .save! displayed the actual error in
> the
> test and made the entire situation clear to me.
>
> My next question is: why would one choose .save/.create over
> .save!/.create! since the former does not render the error when
> testing?
The obvious answer is that you may well have a use case where it's
perfectly OK to attempt to save or at least create an invalid object.
An example would be a user sign-up page where the User object created
from the values put into the form was invalid, but you wanted to just
feed back the validation failures to the user so they could have
another crack at the form. I can imagine some people might do this
with a call to #create! wrapped up in a begin/rescue block, but I've
always found that sort of thing rather clumsy myself. I'd prefer to
see the code check the User#valid? state after a call to #create, then
act accordingly.
Make sense?
Matt
More information about the rspec-users
mailing list