0

我需要测试一个正在运行的回调before_validation,但是因为我只想测试回调,所以我不希望在验证失败时测试失败,此时我得到:

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

我已经尝试了很多东西。我相信我的回调方法在使用时没有被调用,.save(:validate => false)因为它运行在before_validation.

但是当我做这样的事情时:

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

我明白了

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

尽管我在模型中的代码:

before_validation :parse_url, :on => :save

除了方法调用之外,我还想在格式化后检查字段。

我会非常感谢任何帮助。

4

1 回答 1

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.

于 2012-06-14T19:46:45.350 回答