Ruby on rails Rspec控制器测试:未定义方法';订单路径';
编写一些控制器测试,使用Ruby on rails Rspec控制器测试:未定义方法';订单路径';,ruby-on-rails,ruby,rspec,Ruby On Rails,Ruby,Rspec,编写一些控制器测试,使用render\u view检查部分渲染 describe PromoCodeController do render_views describe "GET 'show" do ... a bunch of tests it "renders 'used' partial when promo code has already been used" do @promo_code = create(:promo_code) @user.stub(:pro
render\u view
检查部分渲染
describe PromoCodeController do
render_views
describe "GET 'show" do
... a bunch of tests
it "renders 'used' partial when promo code has already been used" do
@promo_code = create(:promo_code)
@user.stub(:promo_used?).and_return(true)
get 'show', :slug => @promo_code.slug
expect(response).to render_template(:partial => 'promo_code/_used')
end
在所使用的部分
<article>
<p><%= @promo.description.html_safe %></p>
<p>Sorry, it appears this promo code has already been used. Please try again or contact us directly.</p>
<%= link_to "View Order", orders_path(@order), class: "box-button-black", data: { bypass: true } %>
</article>
因此,分部正在查找@订单
。我试着用controller.instance\u variable\u set(:@order,create(:order))
设置它,但在部分设置中它返回为nil
通过在部分视图中添加
的快速测试通过绿色。现在的问题是如何将var@order
传递到所使用的\u部分中。尝试添加规范的类型
我相信动作控制器URL帮助器包含在规范类型中
尝试:
可以根据文件位置设置等级库类型,而不是手动设置等级库类型
# spec_helper.rb
RSpec.configure do |config|
config.infer_spec_type_from_file_location!
end
describe 'GET SHOW' do
run this in a before block
before do
controller.instance_variable_set(:@order, create(:order))
end
it "renders 'used' partial when promo code has already been used" do
promo_code = create(:promo_code)
@user.stub(:promo_used?).and_return(true)
# check if @order variable is assigned in the controller
expect(assigns(:order).to eq order
get 'show', slug: promo_code.slug
expect(response).to render_template(:partial => 'promo_code/_used')
end
end
首先,我需要将其更改为order\u path
,orders\u path
是错误的。Doh
然后我需要存根一些方法来绕过错误
ActionView::Template::Error:
No route matches {:controller=>"order", :action=>"show", :id=>nil}
最后,将方法assign_promo_to_users_order
中的stubing命令分配给当前_用户完成了以下操作:
it "renders 'used' partial when promo code has already been used" do
@promo_code = create(:promo_code)
@user.stub(:promo_used?).and_return(true)
User.any_instance.stub(:assign_promo_to_users_order).and_return(create(:order, :complete))
get 'show', :slug => @promo_code.slug
expect(response).to render_template(:partial => 'promo_code/_used')
end
您是否将orders资源添加到config/routes.rb?它应该是order_路径吗?谢谢David。。。我仍然得到ActionView::Template::Error:没有路由匹配{:controller=>“order”,:action=>“show”,:id=>nil}
。仍然没有将@order交给partialRyan您可以在PromoCodesController中复制并粘贴您的show方法吗?
# spec_helper.rb
RSpec.configure do |config|
config.infer_spec_type_from_file_location!
end
describe 'GET SHOW' do
run this in a before block
before do
controller.instance_variable_set(:@order, create(:order))
end
it "renders 'used' partial when promo code has already been used" do
promo_code = create(:promo_code)
@user.stub(:promo_used?).and_return(true)
# check if @order variable is assigned in the controller
expect(assigns(:order).to eq order
get 'show', slug: promo_code.slug
expect(response).to render_template(:partial => 'promo_code/_used')
end
end
ActionView::Template::Error:
No route matches {:controller=>"order", :action=>"show", :id=>nil}
it "renders 'used' partial when promo code has already been used" do
@promo_code = create(:promo_code)
@user.stub(:promo_used?).and_return(true)
User.any_instance.stub(:assign_promo_to_users_order).and_return(create(:order, :complete))
get 'show', :slug => @promo_code.slug
expect(response).to render_template(:partial => 'promo_code/_used')
end