我在orders / edit.html.erb上有一个页面,我正在使用以下代码进行测试:
require 'spec_helper'
describe "orders/edit.html.erb" do
before(:each) do
@order = FactoryGirl.create(:order)
#assign(:order, stub_model(Order,
# :new_record? => false,
# :email => "MyString",
# :name => "MyString"
#))
end
it "renders the edit order form" do
render :template => "orders/edit", :handlers => [:erb]
end
end
但是,我收到以下测试失败错误消息:
orders/edit.html.erb renders the edit order form
Failure/Error: render :template => "admin/fulfillment/orders/edit", :handlers => [:erb]
ActionView::Template::Error:
undefined method `name' for nil:NilClass
以下是orders / edit.html.erb中发生冲突的地方:
<h3>New Order # <%= @order.number %></h3>
<legend>
<% if @order.user.name && @order.user.waiter.name %>
Diner: <%= @order.user.name %> / Waiter: <%= @order.user.waiter.name %>
<% else %>
No Name available <% end %>
</legend>
<div class="row">
...
更新:这是我的工厂/ order.rb - 我是否也没有生成足够的用户数据,触发零?
FactoryGirl.define do
sequence :number do |n|
n
end
factory :order do
number #{ Factory.next(:number) }
email 'email@e.com'
state 'in_progress'
user { |c| c.association(:user) }
bill_address_id { |c| c.association(:address).id }
ship_address_id { |c| c.association(:address).id }
active true
calculated_at Time.now
completed_at Time.now
after_build {|oi| oi.send(:initialize_state_machines, :dynamic => :force)}
end
factory :in_progress_order, :parent => :order do
state 'in_progress'
end
factory :complete_order, :parent => :order do
state 'complete'
end
我在测试中做错了什么,我需要做些什么才能让它通过?
答案 0 :(得分:0)
您的测试失败,因为@order.user.stylist
为nil
。在if
语句中,您检查@order.user.name
和@order.user.waiter.name
,但不检查@order.user.stylist.name
。
试试这个。
<h3>New Order # <%= @order.number %></h3>
<legend>
<% if @order.user.name && @order.user.waiter.name && @order.user.stylist.name %>
Diner: <%= @order.user.name %> / Waiter: <%= @order.user.stylist.name %>
<% else %>
No Name available
<% end %>
</legend>
<div class="row">
...