使用rspec測試在PUT上使用未定義的方法“祖先”

[英]undefined method `ancestors' on PUT with rspec test


i'm testing with rspec, factory_girl and capybara. The project uses devise, i have the following method to login inside the specs:

我正在測試rspec,factory_girl和capybara。該項目使用devise,我有以下方法登錄規范:

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

Then i perform the tests on companies_controller_spec:

然后我在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" => "r@gmail.com" })
      put :update, {:id => company.to_param, :company => { "email" => "r@gmail.com" }}
    end
  end
end

But i keep getting this two errors:

但我一直得到這兩個錯誤:

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" => "r@gmail.com" })

and

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)

This is my factory for companies:

這是我的公司工廠:

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

2 个解决方案

#1


12  

Just did this myself, accidentally called expect_any_instance_of on an actual instance, instead of on the class itself.

我自己做了這件事,在實際的實例上意外地調用了expect_any_instance_of,而不是在類本身上。

Old question, but for others finding this question, looks like OP should be using Company (uppercase class name) instead of company (lowercase reference to an instance).

老問題,但對於其他人發現這個問題,看起來OP應該使用Company(大寫類名)而不是公司(小寫引用實例)。

expect_any_instance_of(Company).to receive(:update).with({ "email" => "r@gmail.com" })

expect_any_instance_of(Company).to receive(:update).with({“email”=>“r@gmail.com”})

To explain the error in a little more detail, ancestors is trying to access all of the classes from which Company inherits. A good example of ancestors/descendants on this other SO question.

為了更詳細地解釋錯誤,祖先正試圖訪問公司繼承的所有類。在另一個SO問題上,祖先/后代的一個很好的例子。

#2


0  

Since you have company = FactoryGirl.create(:company), isn't company an instance on its own?

既然你有公司= FactoryGirl.create(:company),那么公司本身不是一個實例嗎?

How about Company.any_instance.expects(:update).with({ "email" => "r@gmail.com" })?

Company.any_instance.expects(:update).with({“email”=>“r@gmail.com”})怎么樣?


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2014/09/24/81096639679cfb48522344132534e454.html



 
  © 2014-2022 ITdaan.com 联系我们: