0

I need to test a callback that's run before_validation, but as I want to test only the callback I don't want test to fail when validation fails, and at the moment I get:

ActiveRecord::RecordInvalid:
       Validation failed: Preview Invalid video ID

I've tried out a lot of stuff. I believe that my callback method is not called when using .save(:validate => false) because it runs on before_validation.

But when I do something like this:

show = FactoryGirl.build :show
show.should_receive :parse_url
show.save

I get

Failure/Error: show.should_receive :parse_url
   (#<Show:0x007fd9954699e0>).parse_url(any args)
       expected: 1 time
       received: 0 times

despite my code in the model:

before_validation :parse_url, :on => :save

Also beside the method call I want to check the fields after they were formatted.

I'll be very thankful for any help.

Uko
  • 13,134
  • 6
  • 58
  • 106

1 Answers1

0

Well, you can always just catch the ActiveRecord::RecordInvalid error when it fires, and yet still test the behaviour of your before_validation. Because you catch the error, the RSpec test won't fail.

MrDanA
  • 11,489
  • 2
  • 36
  • 47
  • Thank you. Do you have any idea why is my `FactoryGirl.build`, `.should_receive` and `.save` not working? – Uko Jun 15 '12 at 06:53