首页 文章

RSpec Couldn 't find with ' id'=

提问于
浏览
0

我正在写一个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 "purchases"("id","vendor_id","order_number","status" ,"notes","tradegecko_url","created_at","updated_at") Value (1美元,2美元,3美元,4美元,5美元,6美元,7美元,8美元)退货"id" [0m [["id",4],["vendor_id",4],["order_number","zzz"], ["status","canceled"],["notes","Jean shorts cliche Williamsburg raw denim put a bird on it messenger bag. Shoreditch keytar Brooklyn lomo brunch. Mcsweeney's Cosby Sweater +1 PBR Austin biodiesel freegan."],["tradegecko_url",“_ http://gorczany.info "], [" created_at ", " 2016-07-19 14:51:00.616108 "], [" updated_at ", " 2016-07-19 14:51:00.616108 "]] [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 "发票" (" id ", " purchase_id ", "数", "方面", "货币", " total_due ", " CURRENT_BALANCE ", " DUE_DATE ", "笔记", "状态", " created_at ", "的updated_at ") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12) RETURNING " ID " [[" ID ", 4], [" purchase_id ", 4], ["数", " DD "], ["方面", " TT "], ["货币", " MM "], [" total_due ", " 25000.0 "], [" current_balance ", " 12500.0 "], [" due_date ", " 2015-11-13 "], [" notes ", " Scenester Carles cred quinoa fixie将一只鸟放在上面四个Loko下一级 . 生物柴油副Wayfarers可持续早午餐屠夫locavore . Keytar副下级stumptown Rerry Richardson . "], [" status ", "已取消"], [" created_at ", " 2016-07-19 14:51:00.619066 "], [" updated_at ", " 2016-07-19 14:51:00.619066 "]] [1m[36m (0.1ms)[0m [1mRELEASE SAVEPOINT active_record_1[0m Processing by InvoicesController#new as HTML [1m[35mPurchase Load (0.3ms)[0m SELECT "购买".* FROM "购买" WHERE "购买"." id " = $1 LIMIT 1 [[" id“,nil]]已完成404未找到2ms(ActiveRecord:0.3ms)

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

2 回答

  • 0

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

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

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

  • 0

    工厂中陈述的协会并不持久,因此Purchase.id的 Value 为零 .

    所以在回调之后尝试回调,特征之后,我放弃了使用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
    

    我希望这可以帮助遇到这个问题的任何人 .

相关问题