私は15 の TDD ステップに従って Rails アプリケーションガイドを作成しましたが、解決できない問題に遭遇しました。WordsController の機能テストのために、次のコードを用意しました。
class WordsControllerTest < ActionController::TestCase
test "should get learn" do
get 'learn'
assert_response :success
end
test "learn passes a random word" do
some_word = Word.new
Word.expects(:random).returns(some_word)
get 'learn'
assert_equal some_word, assigns('word')
end
end
Word クラスには、次のコードがあります。
class Word < ActiveRecord::Base
def self.random
all = Word.find :all
all[rand(all.size)]
end
end
テストを実行すると、次のエラーが発生します (簡潔にするために短縮されています)。
1) Failure: unexpected invocation: Word(...).random() satisfied expectations:
- expected exactly once, already invoked once: Word(...).random()
テストの順序を変更して他の多くのことを変更しようとしましたが、何度も何度も同じテストの失敗を受け取り続けます - その Word.random() はすでに呼び出されています。
Rails 3.0 beta 4 と Mocha 0.9.8 を実行しています。問題の解決策を長い間懸命に探してきましたが、見つけられないようです。私は Ruby/Rails を初めて使用するので、言語とフレームワークに慣れていません。
前もって感謝します!