我正在尝试为我的api编写一个请求规范。我使用的是Rspec 3。
我想在ApplicationHelper中存根一个方法。我使用rabl来呈现我的api JSON。
这就是设置
#ApplicationHelper
module ApplicationHelper
  ...
  def preview_url
  end
  ...
end我从我的rabl文件中调用preview_url方法。问题是,由于我使用的是request spec,所以由于我的rspec view specs,我无法访问helper方法
以下是我到目前为止的规范
#spec/request/recipe_spec.rb
require 'rails_helper'
require 'spec_helper'
describe Recipe, 'when viewing the recipe', type: :request,  focus: true do
  ...
  before do
    helper = Object.new.extend(ApplicationHelper)
    allow(helper).to receive(:preview_url).and_return("image.gif")
  end
  it 'some test' do
    ...
  end 
end 但是,这不会存根ApplicationHelper中的实际方法。
我试过了,但没有成功
allow(ApplicationHelper).to receive(:preview_url).and_return("image.gif") -> doesn't work  
allow_any_instance_of(ApplicationHelper).to receive(:preview_url).and_return("image.gif") -> will not work obviously because application helper is a module 如何从我的请求规范中存根ApplicationHelper方法?
发布于 2015-07-24 05:06:19
由于您只是希望存根帮助器方法,而不是测试帮助器本身,因此可以使用view
  before { allow(view).to receive(:preview_url).and_return('image.gif') }
  it 'some test' do
    render
    expect(rendered).to match /Expected Output/
  endhttps://stackoverflow.com/questions/31587475
复制相似问题