gpt4 book ai didi

ruby-on-rails - rspec 使用 draper Decorator 测试 View

转载 作者:行者123 更新时间:2023-12-04 23:57:06 35 4
gpt4 key购买 nike

我尝试用 Rspec 测试我的观点。在我看来,我有一个由 Draper 生成的装饰器。这个装饰器由 decent_exposure gem 公开。

我这样创建我的 rspec 测试:

require 'spec_helper'

describe "boats/show.html.slim" do

let(:boat_decorate) { BoatDecorator.new(get_boat) }
let(:search) { Search.new }

before do
view.stub(:boat_decorate) { boat_decorate }
view.stub(:search) { search }
render :template => 'boats/show.html.slim'
end

it 'should see titlte' do
rendered.should have_selector(:h1, :content => boat_decorate.title)
end
end

在我的助手 stub 中,我生成了 Draper Decorator。在这个 Decorator 中,我有调用一些助手 link_to 的方法。

class BoatDecorator < ApplicationDecorator

decorates :boat

def region_link
h.link_to region_name, '#', :title => region_name
end

end

但是如果我启动这个测试我有一个错误:

  1) boats/show.html.slim should see titlte
Failure/Error: render :template => 'boats/show.html.slim'
ActionView::Template::Error:
undefined method `link_to' for nil:NilClass

我不希望装饰器对所有助手调用 stub 。那我该怎么做呢?

最佳答案

我们需要像 Decorator 规范那样做并在设置中添加:ApplicationController.new.set_current_view_context

require 'spec_helper'

describe "boats/show.html.slim" do

let(:boat_decorate) { BoatDecorator.new(get_boat) }
let(:search) { Search.new }

before do
ApplicationController.new.set_current_view_context
view.stub(:boat_decorate) { boat_decorate }
view.stub(:search) { search }
render :template => 'boats/show.html.slim'
end

it 'should see titlte' do
rendered.should have_selector(:h1, :content => boat_decorate.title)
end
end

关于ruby-on-rails - rspec 使用 draper Decorator 测试 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9681572/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com