0

我在 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">
...

更新:这是我的 factory/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

我在考试中做错了什么,我需要做什么才能通过考试?

4

1 回答 1

0

您的测试失败,因为@order.user.stylistis 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">
...
于 2013-02-15T19:35:58.450 回答