2016-04-21 66 views
1

我在嘗試爲我幫助貢獻的應用編寫測試時遇到問題。我會承認,我的才能絕對在開發的前端,所以在進行rpsec測試時,我並不是最偉大的。如何在rspec中傳遞一個包含消息的對象?

我基本上是創建一個位置的測試,我有一個管理員能夠創建一個新的位置。我還爲這些地點創建了一家工廠。最終,我得到的問題是,當我嘗試1.我風了一個錯誤,說明

'change' requires either an object and message ('change(obj, :msg)') or a block ('change { }'). You passed an object but no message.

增加的位置了數級

我真的不知道我能做些什麼來繞過這個,所以我想知道是否有人可以採取甘德和我做了什麼。

這裏是我廠:

FactoryGirl.define do 
factory :location do |c| 
    c.name 'sample location' 
    c.phone '5555555555' 
    c.fax '5555555555' 
    location_id '123456' 
    association :address, factory: :address 
end 

這裏是我的規格。把那個在{}括號改變對象 -

require 'spec_helper' 

describe Admin::LocationController, type: :controller do 
before(:all) do 
    @admin = create(:admin) 
    @location = create(:location) 
end 

after(:all) do 
    @admin.destroy 
    @location.destroy 
end 

let(:admin) {@admin} 
let(:location) {@location} 

before(:each) do 
    sign_in(:user, admin) 
end 

describe '#create' do 
    it 'creates a new location' do 

    expect{ 
     post :create, location:{ 
     name: 'sample location', 
     phone: '5555555555', 
     fax: '5555555555', 
     location_id: '123456', 

     address_attributes: { 
      address1: '12345 Some St', 
      city: 'Portland', 
      state: 'OR', 
      zip: '91237' 
     } 
     } 
    }.to change(Location.count).by(1) 

    new_location = Location.last 
    expect(new_location.name).to eq 'sample location' 
    expect(new_location.phone).to eq '5555555555' 
    expect(new_location.fax).to eq '5555555555' 
    expect(new_location.location_id). to eq '123456' 
    expect(new_location.address.address1).to eq '12345 Some St' 
    expect(new_location.address.city).to eq 'Portland' 
    expect(new_location.address.state).to eq 'OR' 
    expect(new_location.address.zip).to eq '91237' 
    end 
end 
end 

回答

4

試試這個:

}.to change(Location, :count).by(1) 

OR

}.to change { Location.count }.by(1) 

,而不是

}.to change(Location.count).by(1) 
+0

的備選句法嘿謝謝芽!簡單的語法修改就是我需要的! – kdweber89

-1

的錯誤正是你需要做的說。這可以幫助:

require 'spec_helper' 

    describe Zipadmin::LocationController, type: :controller do 
    before(:all) do 
     @admin = create(:admin) 
     @location = create(:location) 
    end 

    after(:all) do 
     @admin.destroy 
     @location.destroy 
    end 

    let(:admin) {@admin} 
    let(:location) {@location} 

    before(:each) do 
     sign_in(:user, admin) 
    end 

    describe '#create' do 
     it 'creates a new location' do 

     expect{ 
      post :create, location:{ 
      name: 'sample location', 
      phone: '5555555555', 
      fax: '5555555555', 
      location_id: '123456', 

      address_attributes: { 
       address1: '12345 Some St', 
       city: 'Portland', 
       state: 'OR', 
       zip: '91237' 
      } 
      } 
     }.to change{Location.count}.by(1) 

     new_location = Location.last 
     expect(new_location.name).to eq 'sample location' 
     expect(new_location.phone).to eq '5555555555' 
     expect(new_location.fax).to eq '5555555555' 
     expect(new_location.ncpdp_id). to eq '123456' 
     expect(new_location.address.address1).to eq '12345 Some St' 
     expect(new_location.address.city).to eq 'Portland' 
     expect(new_location.address.state).to eq 'OR' 
     expect(new_location.address.zip).to eq '91237' 
     end 
    end 
    end 
1

錯誤是告訴你,你傳遞不正確的參數到change方法。 change方法需要將一個塊傳遞給它。

}.to change { Location.count }.by(1)

它還提到傳遞給它的對象/模型(Location)和一個消息(在符號格式調用一個方法,在這種情況下:count

}.to change(Location, :count).by(1)

相關問題