从请求规范中获取辅助方法 - rspec3

时间:2015-07-23 12:33:16

标签: ruby ruby-on-rails-4 request rabl rspec3

我正在尝试为我的api写一个请求规范。我正在使用Rspec 3.

我想在ApplicationHelper中存根方法。我正在使用rabl来渲染我的api JSON。

这是设置

#ApplicationHelper
module ApplicationHelper
  ...
  def preview_url
  end
  ...
end

我正在调用preview_url文件中的rabl方法。问题是,因为我正在使用request spec我无法访问helper方法给出我的rspec view specs

以下是我的规格到目前为止

#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方法?

1 个答案:

答案 0 :(得分:0)

由于您只是想查找辅助方法而不是测试帮助程序本身,因此您可以使用view

  before { allow(view).to receive(:preview_url).and_return('image.gif') }

  it 'some test' do
    render
    expect(rendered).to match /Expected Output/
  end

Relish Documentation

相关问题