私はこのクラスを持っています:
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
ここで何が欠けていますか?モックを間違って使用していますか?