我有这堂课:
class EnablePost
def initialize(post_klass, id)
raise "oops" if post_klass.blank?
@post_klass = post_klass
@id = id
end
def perform
post = @post_klass.find_by_id(@id)
return unless post
post.update_attribute :enabled, true
end
end
我必须编写的规范来测试上述内容:
describe EnablePost do
it "should enable a post" do
post = mock
post.should_receive(:blank?).and_return(false)
post.should_receive(:find_by_id).with(22).and_return(post)
post.should_receive(:update_attribute).with(:enabled, true)
result = EnablePost.new(Post, 22).perform
result.should be_true
end
end
但我真正想做的是把EnablePost
它当作一个黑匣子。我不想嘲笑:blank?
,:find_by_id
或:update_attribute
. 也就是说,我希望我的规范看起来像:
describe EnablePost do
it "should enable a post" do
post = mock
result = EnablePost.new(post, 22).perform
result.should be_true
end
end
我在这里想念什么?我是否错误地使用了模拟?