我一直在遵循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
Run Code Online (Sandbox Code Playgroud)
在Word类中,我有以下代码:
class Word < ActiveRecord::Base
def self.random
all = Word.find :all
all[rand(all.size)]
end
end
Run Code Online (Sandbox Code Playgroud)
当我运行测试时,我遇到以下错误(为简洁起见缩短):
1) Failure: unexpected invocation: Word(...).random() satisfied expectations:
- expected exactly once, already invoked once: Word(...).random()
Run Code Online (Sandbox Code Playgroud)
我已经尝试过更改测试顺序以及其他许多内容,但是我一次又一次地继续接收相同的测试失败 - 已经调用了Word.random().
我正在运行Rails 3.0 beta 4和Mocha 0.9.8.我一直在努力寻找解决问题的方法,但我似乎无法找到它.我是Ruby/Rails的新手,所以我对语言和框架并不熟悉.
提前致谢!