Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何在Rails 4中测试助手?_Ruby On Rails_Ruby_Ruby On Rails 4_Rspec - Fatal编程技术网

Ruby on rails 如何在Rails 4中测试助手?

Ruby on rails 如何在Rails 4中测试助手?,ruby-on-rails,ruby,ruby-on-rails-4,rspec,Ruby On Rails,Ruby,Ruby On Rails 4,Rspec,使用此ApplicationHelper: class ApplicationHelper def my_method link_to 'foo', 'bar' end end 这个应用程序\u助手\u规范: require 'rails_helper' describe ApplicationHelper do describe 'links' do it 'should call a helper method' do expect(hel

使用此
ApplicationHelper

class ApplicationHelper 
  def my_method
    link_to 'foo', 'bar'
  end  
end
这个
应用程序\u助手\u规范

require 'rails_helper'

describe ApplicationHelper do
  describe 'links' do 
    it 'should call a helper method' do
      expect(helper.my_method).to eq("<a href='bar'>foo</a>")
    end
  end
end
如果我这样做:

require 'rails_helper'
include ApplicationHelper

describe ApplicationHelper do
  describe 'links' do 
    it 'should call a helper method' do
      expect(my_method).to eq("<a href='bar'>foo</a>")
    end
  end
end
(后一种情况与我在
rails\u helper
中定义
config.include ApplicationHelper
相同)


显然,规范环境不包括所有标准Rails帮助程序。我做错了什么

您需要启用
从文件位置推断规格类型选项,或显式设置测试类型,例如:


描述ApplicationHelper,类型::helper do
...
结束

您需要启用
从文件位置推断规格类型选项,或显式设置测试类型,例如:


描述ApplicationHelper,类型::helper do
...
结束

我认为关于在规范中定义
类型::helper
是正确的,因为它将解决
未定义的局部变量或方法“helper”的问题

然而,关于“我如何测试Rails 4中的帮助者”的一个总体问题,特别是你的方法似乎只是打了一个电话,假设你不想在一个特性规范中覆盖这个,并想单独测试它,那么如果你想测试它的话,就考虑一下。“调用

#my_方法
返回一个包含foo-bar链接HTML的字符串”,将确认调用
链接到'foo','bar'
将返回

因此,我建议将您的规格提高一个级别,说您想测试“调用
#my_方法
会返回foo-bar的链接(无论Rails以何种方式将链接传递给我)”:

但就个人而言,我认为这种方法没有足够的逻辑来保证在helper规范中单独进行测试,最好在特性规范中介绍它,我假设您将测试链接的显示,或者单击它查看发生了什么等。

我认为关于在您的应用程序上定义
type::helper
规范是正确的,因为它将解决
未定义的局部变量或方法“helper”的问题

然而,关于“我如何测试Rails 4中的帮助者”的一个总体问题,特别是你的方法似乎只是打电话,假设你不想在一个特性规范中覆盖这个,并且想孤立地测试它,那么如果你想测试它的话,就考虑一下。调用

#my_方法
返回一个包含foo-bar链接HTML的字符串”,将确认调用
link_到'foo','bar'
将返回

因此,我建议将您的规格提高一个级别,说您想测试“调用
#my_方法
会返回foo-bar的链接(无论Rails以何种方式将链接传递给我)”:


但就我个人而言,我认为这种方法没有足够的逻辑来保证在助手规范中单独进行测试,最好在特性规范中对其进行介绍,我假设您将测试链接的显示,或者单击它来查看发生了什么等。

您需要在Rails中加载Rails环境的规范助手吗lper?你应该看看你能不能在github上推动你的项目我很确定你做错了什么。你需要在Rails\u helper中加载Rails环境的spec helper吗?你应该看看你能不能在github上推动你的项目我很确定你做错了什么。谢谢@Paul——我应该澄清一下我的问题estion只是一个简单的例子。这里讨论的实际方法有更多的逻辑性,足以值得进行实际测试。谢谢@Paul--我应该澄清我的问题只是一个简单的例子。这里讨论的实际方法有更多的逻辑性,足以值得进行实际测试。谢谢@Andy--
似乎是我在这里缺少的东西(在
rails\u helper
中)。谢谢你@Andy--
从文件位置推断规范类型!
似乎是我在这里缺少的东西(在
rails\u helper
中)。
require 'rails_helper'
include ApplicationHelper

describe ApplicationHelper do
  describe 'links' do 
    it 'should call a helper method' do
      expect(my_method).to eq("<a href='bar'>foo</a>")
    end
  end
end
undefined method `link_to' for #<RSpec::ExampleGroups::ApplicationHelper::Links:0x007fda1c4c3e90>
require 'rails_helper'

RSpec.describe ApplicationHelper, type: :helper do
  describe '#my_method' do
    it 'returns a foo bar link' do
      expect(helper).to receive(:link_to).with('foo', 'bar')
      helper.my_method
    end
  end
end