i'm testing with rspec, factory_girl and capybara. The project uses devise, i have the following method to login inside the specs:
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:
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
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" => "[email protected]" })
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