RSpec无法找到' id' =

时间:2016-07-18 15:20:51

标签: ruby-on-rails rspec-rails

我正在编写RSpec控制器测试,但我遇到了以下问题。

这种关系是发票属于购买,而购买有很多发票。

我的控制器有:

class InvoicesController < ApplicationController
  def index
    @invoices = Invoice.all
  end

  def new
    @purchase = Purchase.find(params[:purchase])
    @invoice = Invoice.new(:purchase_id => params[:purchase])
  end

我的工厂有:

FactoryGirl.define do
  factory :invoice do |f|
    sequence(:id) { |number| number }
    f.purchase_id {rand(1..30)}
    f.number { FFaker::String.from_regexp(/\A[A-Za-z0-9]+\z/) }
    f.terms { FFaker::String.from_regexp(/\A[A-Za-z0-9\s]+\z/) }
    f.currency { FFaker::String.from_regexp(/\A[A-Z]+\z/) }
    f.total_due {'25000.00'}
    f.current_balance {'12500.00'}
    f.due_date { FFaker::Time.date }
    f.notes { FFaker::HipsterIpsum.paragraph }
    f.status {[:open, :paid, :canceled].sample}
    purchase
  end

  factory :invalid_invoice, parent: :invoice do |f|
    f.status nil
  end
end

我的控制器规范(只是有问题的部分)有:

describe "GET new" do
    it "assigns a new invoice to @invoice" do
      invoice = FactoryGirl.create(:invoice)
      get :new
      expect(assigns(:invoice)).to_not eq(invoice)
    end

    it "renders the :new template" do
      get :new
      expect(response).to render_template :new
    end
  end

在我的路线中,我有:

purchases GET                       /purchases(.:format)                        purchases#index
                                 POST                      /purchases(.:format)                        purchases#create
                    new_purchase GET                       /purchases/new(.:format)                    purchases#new
                   edit_purchase GET                       /purchases/:id/edit(.:format)               purchases#edit
                        purchase GET                       /purchases/:id(.:format)                    purchases#show
                                 PATCH                     /purchases/:id(.:format)                    purchases#update
                                 PUT                       /purchases/:id(.:format)                    purchases#update
                                 DELETE                    /purchases/:id(.:format)                    purchases#destroy

invoices GET                       /invoices(.:format)                         invoices#index
                                 POST                      /invoices(.:format)                         invoices#create
                     new_invoice GET                       /invoices/new(.:format)                     invoices#new
                    edit_invoice GET                       /invoices/:id/edit(.:format)                invoices#edit
                         invoice GET                       /invoices/:id(.:format)                     invoices#show
                                 PATCH                     /invoices/:id(.:format)                     invoices#update
                                 PUT                       /invoices/:id(.:format)                     invoices#update
                                 DELETE                    /invoices/:id(.:format)                     invoices#destroy

当我运行测试时,我得到了这个:

1) InvoicesController GET new assigns a new invoice to @invoice
     Failure/Error: get :new
     ActiveRecord::RecordNotFound:
       Couldn't find Purchase with 'id'=
     # ./app/controllers/invoices_controller.rb:7:in `new'
     # ./spec/controllers/invoices_controller_spec.rb:38:in `block (3 levels) in <top (required)>'

  2) InvoicesController GET new renders the :new template
     Failure/Error: get :new
     ActiveRecord::RecordNotFound:
       Couldn't find Purchase with 'id'=
     # ./app/controllers/invoices_controller.rb:7:in `new'
     # ./spec/controllers/invoices_controller_spec.rb:43:in `block (3 levels) in <top (required)>'

以下是test.log

的摘录

[1m [36m(0.1ms)[0m [1mRELEASE SAVEPOINT active_record_1 [0m]   [1m [35m(0.1ms)[0m SAVEPOINT active_record_1   [1m [36mSQL(0.3ms)[0m [1mINSERT INTO&#34;购买&#34; (&#34; id&#34;,&#34; vendor_id&#34;,&#34; order_number&#34;,&#34; status&#34;,&#34; notes&#34;,&#34; tradegecko_url&#34;,&#34; created_at&#34;,&#34; updated_at&#34;)VALUES($ 1,$ 2,$ 3,$ 4,$ 5,$ 6,$ 7,$ 8)返回&#34; id&#34; [0m [[&#34; id&#34;,4],[&#34; vendor_id&#34;,4],[&#34; order_number&#34;,&#34; zzz&#34;],[ &#34;状态&#34;,&#34;取消&#34;],[&#34;注意&#34;,&#34; Jean短裤陈词滥调威廉斯堡原始牛仔布将一只鸟放在它的斜挎包上。 Shoreditch keytar Brooklyn lomo早午餐。 Mcsweeney的Cosby毛衣+1 PBR Austin生物柴油freegan。&#34;],[&#34; tradegecko_url&#34;,&#34; http://gorczany.info&#34;],[&#34; created_at&#34;,&#34; 2016-07-19 14:51:00.616108&#34;],[&#34; updated_at&#34;,&#34; 2016-07-19 14:51:00.616108& #34;]]   [1m [35m(0.1ms)[0m RELEASE SAVEPOINT active_record_1   [1m [36m(0.1ms)[0m [1mSAVEPOINT active_record_1 [0m   [1m [35mSQL(0.3ms)[0m INSERT INTO&#34;发票&#34; (&#34; id&#34;,&#34; purchase_id&#34;,&#34; number&#34;,&#34; terms&#34;,&#34; currency&#34;,&#34; total_due&#34;,&#34; current_balance&#34;,&#34; due_date&#34;,&#34; notes&#34;,&#34; status&#34;,&#34; created_at&#34;, &#34; updated_at&#34;)VALUES($ 1,$ 2,$ 3,$ 4,$ 5,$ 6,$ 7,$ 8,$ 9,$ 10,$ 11,$ 12)返回&#34; id&#34; [[&#34; id&#34;,4],[&#34; purchase_id&#34;,4],[&#34;数字&#34;,&#34; dd&#34;],[&# 34;术语&#34;,&#34; TT&#34;],[&#34;货币&#34;,&#34; MM&#34;],[&#34; total_due&#34;,&#34 ; 25000.0&#34;],[&#34; current_balance&#34;,&#34; 12500.0&#34;],[&#34; due_date&#34;,&#34; 2015-11-13&#34 ;],[&#34;音符&#34;,&#34; Scenester Carles信任quinoa fixie将一只鸟放在它上面四个Loko下一级。生物柴油副Wayfarers可持续早午餐屠夫locavore。 Keytar副下级stumptown Rerry Richardson。&#34;],[&#34;状态&#34;,&#34;取消&#34;],[&#34; created_at&#34;,&#34; 2016- 07-19 14:51:00.619066&#34;],[&#34; updated_at&#34;,&#34; 2016-07-19 14:51:00.619066&#34;]]   [1m [36m(0.1ms)[0m [1mRELEASE SAVEPOINT active_record_1 [0m] 由InvoicesController处理#new为HTML   [1m [35m购买负荷(0.3ms)[0m SELECT&#34;购买&#34;。* FROM&#34;购买&#34;购买&#34;。&#34; id&#34; = $ 1 LIMIT 1 [[&#34; id&#34;,nil]] 完成404未找到2ms(ActiveRecord:0.3ms)

我认为问题在于工厂关联已创建,但未保存。因此,当调用purchase.id时,它返回nil。

2 个答案:

答案 0 :(得分:0)

您正尝试在new操作中加载购买:

  def new
    @purchase = Purchase.find(params[:purchase])
    @invoice = Invoice.new(:purchase_id => params[:purchase])
  end

但是在你的测试中,你没有传递:purchase参数(应该是:我猜是buy_id)!

答案 1 :(得分:0)

工厂中陈述的协会并未持续存在,导致Purchase.id的价值为零。

所以在回调之后尝试回调,特征之后,我放弃了使用FactoryGirl自动生成的关联。

最后,这是我必须做的事情:

我从工厂中删除了所有声明的关联,然后手动完成了规范中编辑的代码:

describe "GET new" do

    it "assigns a new invoice to @invoice" do
      invoice = FactoryGirl.create(:invoice)
      get :new, purchase: FactoryGirl.create(:purchase)
      expect(assigns(:invoice)).to_not eq(invoice)
    end

    it "renders the :new template" do
      get :new, purchase: FactoryGirl.create(:purchase)
      expect(response).to render_template :new
    end
  end

我希望这可以帮助遇到此问题的其他人。