我正在努力学习 rspec。我似乎无法测试 Rails Controller 方法。当我在测试中调用该方法时,rspec 只是返回一个未定义的方法错误。这是我的测试例子
it 'should return 99 if large' do
GamesController.testme(1000).should == 99
end
这里是错误:
Failure/Error: GamesController.testme(1000).should == 99
NoMethodError:
undefined method `testme' for GamesController:Class
我在 GamesController 中确实有一个 testme 方法。我不明白为什么测试代码看不到我的方法。
感谢任何帮助。
最佳答案
我认为正确的做法是这样的:
describe GamesController do
it 'should return 99 if large' do
controller.testme(1000).should == 99
end
end
在 Rails Controller 规范中,当您将 Controller 类放在 describe
中时,您可以使用 controller
方法来获取实例 :P
显然,如果 testme
方法是私有(private)的,你仍然必须使用 controller.send :testme
关于ruby-on-rails - 如何使用 rspec 测试 Controller 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10051686/