我从Rspec
收到此错误消息Failure/Error: @address = @owner.addresses.build
NoMethodError:
undefined method `build' for #<Array:0x007f9faba657f0>
以下是我的控制器的片段
class AddressesController < ApplicationController
def new
@owner = Owner.find(params[:owner_id])
@address = @owner.addresses.build
end
end
以下是我的规范文件中的代码段:
describe "GET #new" do
let(:owner) { create(:owner) }
before { xhr :get, :new, owner_id: owner.id }
it 'response will be success' do
expect(response).to be_success
end
end
以下是addresses#new
new_owner_address GET /owners/:owner_id/addresses/new(.:format) addresses#new
addresses.build
方法在console
中工作正常,并且在我的应用程序中工作正常,但它在规范上失败了。任何建议将受到高度赞赏。
更新
工厂:
FactoryGirl.define do
factory :owner do
name 'foo'
address 'bar'
amount 200.00
country 'foobar'
state 'qax'
end
end
模型和关联:
class Owner < ActiveRecord::Base
has_many :addresses, dependent: :destroy
end
class Address < ActiveRecord::Base
belongs_to :owner
end
答案 0 :(得分:0)
@owner.addresses
必须是ActiveRecord::Relation
才能回复#build
。但是,它似乎在您的规范中返回Array
。您的工厂是否以某种方式覆盖默认关联?