加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 如何使用rspec测试控制器方法?

发布时间:2020-12-16 20:22:32 所属栏目:百科 来源:网络整理
导读:我正在尝试学习rspec.我似乎无法测试轨道控制器的方法.当我在测试中调用该方法时,rspec只是返回一个未定义的方法错误.这是我的测试例子 it 'should return 99 if large' do GamesController.testme(1000).should == 99end 这里是错误: Failure/Error: Games
我正在尝试学习rspec.我似乎无法测试轨道控制器的方法.当我在测试中调用该方法时,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中有一个测试方法.我不明白为什么测试代码看不到我的方法.

任何帮助是赞赏.

解决方法

我认为正确的方法是这样的:
describe GamesController do
  it 'should return 99 if large' do
    controller.testme(1000).should == 99
  end
end

在rails控制器规范中,当将controller类放在describe中时,可以使用controller方法获取一个实例:P显然,如果testme方法是私有的,你仍然需要使用controller.send:testme

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读