【问题标题】:nested resource not working testing view rspec嵌套资源不工作测试视图 rspec
【发布时间】:2014-08-12 20:04:49
【问题描述】:

我对规范视图 rspec 中的嵌套资源有一些问题,

我有这样的路线:

resources :customers do
  resources :charges
end

这是我使用 rspec 进行的视图测试:

require 'rails_helper'

RSpec.describe "charges/index", :type => :view do
  before(:each) do
    assign(:customers, [
      @customer1 = Customer.create!(
        :first_name => "tardjo",
        :last_name => "ea...ea...ea"
      ),
      @customer2 = Customer.create!(
        :first_name => "udin",
        :last_name => "penyok"
      )
    ])

    assign(:charges, [
      Charge.create!(
        :created => 1,
        :paid => false,
        :amount => 1.5,
        :currency => "Currency",
        :refunded => false,
        :disputed => false,
        :customer_id => @customer1.id
      ),
      Charge.create!(
        :created => 1,
        :paid => false,
        :amount => 1.5,
        :currency => "Currency",
        :refunded => false,
        :disputed => false,
        :customer_id => @customer2.id
      )
    ])
  end

  it "renders a list of charges" do
    render
    assert_select "tr>td", :text => 1.to_s, :count => 2
    assert_select "tr>td", :text => false.to_s, :count => 2
    assert_select "tr>td", :text => 1.5.to_s, :count => 2
    assert_select "tr>td", :text => "Currency".to_s, :count => 2
    assert_select "tr>td", :text => false.to_s, :count => 2
    assert_select "tr>td", :text => 2.to_s, :count => 2
    assert_select "tr>td", :text => false.to_s, :count => 2
  end
end

但我得到这样的错误:

失败/错误:渲染

ActionView::Template::错误: 没有路由匹配 {:customer_id=>nil, :id=>1} 缺少必需的键:[:customer_id]

卡在这里

【问题讨论】:

    标签: ruby-on-rails rspec


    【解决方案1】:

    这很可能是一个范围界定问题

    试试:

    # these variables might need to be defined outside of the `:customers` array
    @customer1 = Customer.create!(
        :first_name => "tardjo",
        :last_name => "ea...ea...ea"
    )
    @customer2 = Customer.create!(
        :first_name => "udin",
        :last_name => "penyok"
    )
    assign(:customers, [  @customer1, @customer2  ])
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-06-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多