2014-09-24 37 views
4

我正在测试rspec,factory_girl和水豚。该项目采用设计,我有以下方法的规格内登录:未定义的方法`祖先'与rspec测试的PUT

def login_admin 
before(:each) do 
    @request.env["devise.mapping"] = Devise.mappings[:admin] 
    sign_in FactoryGirl.create(:admin) 
end 
end 

def login_user 
before(:each) do 
    @request.env["devise.mapping"] = Devise.mappings[:user] 
    sign_in FactoryGirl.create(:user) 
end 

end 

然后我就companies_controller_spec进行测试:

require 'spec_helper' 

describe CompaniesController, :type => :controller do 

let(:valid_attributes) { { "email" => Faker::Internet.email } } 

login_admin 

describe "GET show" do 

    it "assigns the requested company as @company" do 
    company = FactoryGirl.create(:company) 
    get :show, {:id => company.to_param} 
    expect(assigns(:company)).to eq(company) 
    end 
end 

describe "GET edit" do 
    it "assigns the requested company as @company" do 
    company = FactoryGirl.create(:company) 
    get :edit, {:id => company.to_param} 
    expect(assigns(:company)).to eq(company) 
    end 
end 

describe "PUT update" do 
    describe "with valid params" do 
    it "updates the requested company" do 
     company = FactoryGirl.create(:company) 
     expect_any_instance_of(company).to receive(:update).with({ "email" => "[email protected]" }) 
     put :update, {:id => company.to_param, :company => { "email" => "[email protected]" }} 
    end 
    end 
end 

但我不断收到这两个错误:

NoMethodError: 
    undefined method `ancestors' for #<Company:0x000000059b41f0> 
# ./spec/controllers/companies_controller_spec.rb:34:in `block (4 levels) in <top (required)>' 
line 34: expect_any_instance_of(company).to receive(:update).with({ "email" => "[email protected]" }) 

expected: #<Company id: 86... 
got: nil 
# ./spec/controllers/companies_controller_spec.rb:41:in `block (4 levels) in <top (required)>' 
line 41: expect(assigns(:company)).to eq(company) 

这是我公司的工厂:

FactoryGirl.define do 
    factory :company do 
    name { Faker::Name.name } 
    plan_id {} 
    phone { Faker::PhoneNumber.phone_number } 
    email { Faker::Internet.email } 
    facebook { Faker::Internet.url('facebook.com') } 
    twitter { Faker::Internet.url('twitter.com') } 
    linkedin { Faker::Internet.url('linkedin.com') } 
    web { Faker::Internet.url } 
end 
end 
+0

堆栈跟踪会很好:) – 2014-09-24 17:49:17

+0

已添加@KyleMacey!主要问题。 – ntonnelier 2014-09-24 18:03:39

回答

0

既然你有company = FactoryGirl.create(:company),是不是company自身的一个实例?

Company.any_instance.expects(:update).with({ "email" => "[email protected]" })怎么样?

9

刚做了这个自己,不小心在实际实例上调用了expect_any_instance_of,而不是在类本身上。

老问题,但对于其他人发现这个问题,看起来像OP应该使用Company(大写的类名),而不是company(小写引用的实例)。

expect_any_instance_of(Company).to receive(:update).with({ "email" => "[email protected]" })

为了解释更详细一点的错误,ancestors试图访问所有类从Company继承的。祖先/后代on this other SO question的一个很好的例子。

+1

我使用了一点点不同的堆栈(RSpec和Fabrication),但得到了相同的错误。我曾经在一个对象上调用了'allow_any_instance_of',而不是一个类。这为我做了。谢谢! – 2016-01-26 00:40:06

+1

这是我的问题的正确解决方案。我以'WhateverClass.new'作为主题,使用'expect_any_instance_of'。 – dannypaz 2016-04-04 14:11:26

相关问题