Undefined ancestor method on PUT with rspec check

I am testing rspec, factory_girl and capybara. The project uses devise, I have the following way to enter the specification:

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 run tests on company_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 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
+4
source share
2 answers

I just did it myself, accidentally called it expect_any_instance_ofon the actual instance, and not on the class itself.

An old question, but for others who find this question, it looks like the OP should use Company(uppercase name) instead of Company(lowercase link to instance).

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

, ancestors , Company. / SO.

+10

company = FactoryGirl.create(:company), company ?

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

0

All Articles