2016-04-21 11 views
1

Ich habe Probleme beim Versuch, einen Test für eine App zu schreiben, zu der ich einen Beitrag leisten kann. Ich werde im Vorfeld zugeben, dass meine Talente definitiv am vorderen Ende der Entwicklung stehen, und wenn es um das Testen von rpsec geht, bin ich nicht der Größte.Wie übergibt man ein Objekt mit einer Nachricht in rspec?

Ich bin im Grunde erstellen einen Test für einen Standort, und ich habe einen Administrator kann einen neuen Speicherort erstellen. Ich habe auch eine Fabrik für diese Orte geschaffen. Letztlich ist das Problem, das ich erhalte, wenn ich versuche ich mit einem Fehler, der besagt weiß ich wirklich

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

aufzuwickeln von 1. Zählpegels eines Standorts bis zu erhöhen nicht, was ich tun kann, um umgehen Sie das, und so frage ich mich, ob jemand einen Blick riskieren könnte und was ich getan habe.

Hier ist meine Fabrik:

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 

Hier ist mein spec.

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 

Antwort

4

Try this:

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

ODER

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

statt

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

Hey danke Knospe !! Diese einfache Syntaxänderung war alles was ich brauchte! – kdweber89

-1

Der Fehler sagt genau das, was Sie tun müssen - Objekt zu setzen, die in {} Klammern verändert. Dies könnte helfen:

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

Der Fehler ist Ihnen zu sagen, dass Sie eine falsche Argumente sind vorbei zu der change Methode. Die Methode change erfordert, dass ein Block an sie übergeben wird.

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

Es erwähnt auch eine alternative Syntax es ein Objekt/Modell vorbei (Location) und eine Nachricht (ein Verfahren, in Symbolformat zu nennen, in diesem Fall :count)

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

Verwandte Themen