Stack:Rails 3.0.7,Mongoid 2.2.5,RSpec 2.11.0,RR 1.0.4
我有一个如下所示的订阅模型:
class Subscription
include Mongoid::Document
embeds_many :addons
after_save :update_feature_policy!
def update_feature_policy!
puts "Updating feature policy!"
end
end
我有一个看起来像这样的规范:
it "should update FeaturePolicy when adding an addon" do
puts "Starting spec"
mock(subscription).update_feature_policy! { true }
subscription.addons << user_addon
puts "Finished spec"
end
规范失败,但我在控制台中看到了这个输出:
Starting spec
Updating feature policy!
Finished spec
Failure/Error: mock(subscription).update_feature_policy! { true }
RR::Errors::TimesCalledError:
update_feature_policy!()
Called 0 times.
Expected 1 times.
是什么导致模拟对象不捕获方法调用并传递规范?
答案 0 :(得分:0)
回调似乎是重新查找记录并创建单独的订阅实例(因此不会访问模拟对象)。我不确定为什么 - 似乎它不是很高效 - 但我找到了一个解决方法:
it "should update FeaturePolicy when adding an addon" do
puts "Starting spec"
mock.instance_of(Subscription).update_feature_policy! { true }
subscription.addons << user_addon
puts "Finished spec"
end
这将检查在update_feature_policy!
的任何实例上调用的Subscription
。不是很好,但为了我的目的,它可行。
如果有人能够回答为什么会这样,或者提供更好的解决方案,请做。