如何在rspec中同时传递对象和消息?

kdw*_*r89 2 rspec ruby-on-rails

我在尝试为我提供帮助的应用编写测试时遇到了麻烦。我先要承认,我的才能绝对在开发的前端,因此,在rpsec测试方面,我并不是最出色的。

我基本上是在为位置创建测试,并且我的管理员能够创建一个新位置。我还为这些地点创建了一家工厂。最终,我遇到的问题是,当我尝试将位置的计数级别增加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
Run Code Online (Sandbox Code Playgroud)

这是我的规格。

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
Run Code Online (Sandbox Code Playgroud)

Cri*_*nça 5

尝试这个:

}.to change(Location, :count).by(1)
Run Code Online (Sandbox Code Playgroud)

要么

}.to change { Location.count }.by(1)
Run Code Online (Sandbox Code Playgroud)

代替

}.to change(Location.count).by(1)
Run Code Online (Sandbox Code Playgroud)