-
Notifications
You must be signed in to change notification settings - Fork 57
Controller Testing
During its usual run, the Reservations app has a number of barriers, without the setup of which it will lock you out of much functionality (i.e. redirect you to go and set up the app).
Every controller checks that AppConfig exists and current_user has (whatever) privileges. Therefore, preface your controller tests with
before(:all) do
@app_config = FactoryGirl.create(:app_config)
end
before(:each) do
@controller.stub(:first_time_user).and_return(FactoryGirl.create(:user))
@controller.stub(:current_user).and_return(FactoryGirl.create(:admin)) # Or whatever other role
end
This is because unless Reservations detects a valid AppConfig and a user with proper authorization, it will redirect to a set-up page (or bad-authentication page). That is, however, not immediately clear from the tests failing.
It is worth noting, too, that FactoryGirl cleans up after itself; consequently, it is not necessary to add an after(:all) { @app_config.destroy }
hook. In fact, it breaks the database state (for some reason), so don't do it.
The Cart
object lives in session[:cart]. In order to set it, it needs to be passed as a third argument to the HTTP request verb, e.g.
get :new, nil, { cart: FactoryGirl.build(:cart_with_items, reserver_id: @user.id) }