ruby-on-rails - 如何使用 rspec 测试 Controller 方法?

标签 ruby-on-rails testing methods rspec controller

我正在努力学习 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/

相关文章:

unit-testing - 我怎样才能测试变化的东西?

ruby-on-rails - rails : Good process for adding tests retroactively?

C# 代码列出从起始方法调用的所有方法?

Java,如何从二维 double 组中查找特定值?

ruby-on-rails - 将图像上传到云服务的最简单解决方案

ruby-on-rails - 全新安装 Mountain Lion 的 RailsInstaller 问题

ruby-on-rails - rspec & 设计测试助手

ruby-on-rails - Rails 6 迁移在现有表之间添加外键关联

php - "No tests executed"我根本无法在 NetBeans 中运行任何 PHP 测试

java - java ab中动态绑定(bind)和签名方法的问题