クラスとスペックがあります。
class Store
def activate(product_klass, product_id)
product = product_klass.find(product_id)
if product.inactive?
product.update_attribute :active, true
end
end
end
describe Store do
it "should activate an inactive product" do
product = mock
product.stub(:inactive?).and_return(true)
store = Store.new
store.activate(22) #
product.should be_active
end
end
仕様の実行に失敗します。私は得る:
Mock received unexpected message :find_by_id with (1)
それを満たすため product.should_receive(:find_by_id).with(1).and_return(product)
に、行の前にを追加しますstore.activate(product, 22)
。(テストしているメソッドの内部についてテストにあまり知られたくないので、これは間違っているようです)
仕様を再度実行するfalse
と、期待される代わりに次の行が返される失敗が発生しますtrue
。
product.should be_active
つまり、実際には に設定されていないため、返されますfalse
。モックに吸収されただけです。product.update_attribute :active, true
active
true
たくさんの質問があります。rspec'cing はどのように行うのですか? 代わりにこれをどのようにテストする必要がありましたか? モックとスタブを正しく使用していますか?
どんな助けでも大歓迎です。