Reputation: 2502
I've integrated Devise with my RoR app and am now trying to test my Controllers, specifically the one that routes me to my root_url
.
I've used this HOWTO on Devise's page to setup my admin/user Factories, but there is an additional component that is part of my user signup process, which is creating a Company
.
So:
User
: has_one :company
Company
: has_many :users
The flow for a new user looks like this:
Company
information and submitsPages#home
(which is my root_url
)Using Devise's HOWTO, I created a ControllerHelpers
file within Support
:
module ControllerHelpers
def login_user
before(:each) do
@request.env["devise.mapping"] = Devise.mappings[:user]
user = FactoryGirl.create(:user)
user.confirm # or set a confirmed_at inside the factory. Only necessary if you are using the "confirmable" module
sign_in user
end
end
end
I suspect there is something wrong with my User Factory
since it doesn't seem like a Company
is being created, but I'm so new to RSpec, I'm unsure.
FactoryGirl.define do
factory :user do
first_name "Test"
last_name "User"
full_name "Test User"
email "[email protected]"
phone_number "111-222-3333"
terms_accepted true
time_zone "Central Time (US & Canada)"
password "password"
password_confirmation "password"
confirmed_at Date.today
association :company
end
end
And I have a company.rb
factory as well:
FactoryGirl.define do
factory :company do
id 1
name "ACME Test"
address_1 "123 Shady Lane."
address_2 "Suite 400"
city "Testville"
state "Test"
zip_code "12345"
has_payment_plan false
stripe_id "cus_34d434343e4e3e3"
locked false
end
end
My pages_controller_spec.rb
is simple at this point:
require 'rails_helper'
RSpec.describe PagesController, :type => :controller do
describe "User: GET #home" do
login_user
it "signs in the user" do
expect(response).to render_template(:home)
end
end
end
This results in the following RSpec error:
1) PagesController User: GET #home signs in the user
Failure/Error: expect(response).to render_template(:home)
expecting <"home"> but was a redirect to <http://test.host/companies/new>
# ./spec/controllers/pages_controller_spec.rb:10:in `block (3 levels) in <top (required)>'
So, it's not even doing the render_template
portion of my test?
UPDATE: Added Home Controller
controllers/pages_controller#home
def home
if current_user && current_user.company
verify_subscription
get_company_and_locations
get_network_hosts
get_network_hosts_at_risk
@network_hosts_snip = @network_hosts_at_risk.sort_by{ |h| -h.security_percentage }.first(5)
get_company_issues
@issues = @issues.sort_by{ |i| -i.cvss_score }.first(5)
@deferred_issues = @company.deferred_issues.last(5)
@deferred_hosts = @company.deferred_hosts.last(5)
else
redirect_to new_company_path
end
end
Upvotes: 1
Views: 385
Reputation: 2401
As we found out together in the chat... Your company
was created but wasn't persisted to DB. It's because you had strategy: :build
in your factory.
strategy: :build
means that your association object will be created but won't be persisted to DB. To persist it you should use strategy: :create
. Or in your use case you can replace association :company, strategy: :build
with just company
. FactoryGirl is smart enough to recognize it as an association which must be created and persisted.
And you need to set up FactoryGirl association between company
and subscription
the same way.
Upvotes: 1