rails - Objects persisted by FactoryGirl are not available in Controller - ruby-on-rails

I am writing tests for my controllers in admin namespace. Using RSpec (3.5.0), FactoryGirl (4.8.0), DatabaseCleaner (1.5.3) and Mongoid (6.0.3).
The problem is that these test act strangely. When testing GET index request, objects produced by FactoryGirl are successfully created and persisted. However, the controller does not seem to find them.
I have three different controllers. 2 out of 3 have this problem and the third one works like a charm. The code is the same (except for naming) the only difference is that the resource for the working controller is nested.
The one for accessories works:
describe "GET #index", get: true do
let (:accessory) { FactoryGirl.create(:accessory) }
before do
get :index, params: { category_id: accessory.category_id.to_s }, session: valid_session, format: :json
end
it "responses with OK status" do
expect(response).to have_http_status(:success)
end
it "responses with a non-empty Array" do
expect(json_body).to be_kind_of(Array)
expect(json_body.length).to eq(1)
end
it "responses with JSON containing accessory" do
expect(response.body).to be_json
expect(json_body.first.with_indifferent_access).to match({
id: accessory.to_param,
name: 'Test accessory',
description: 'This is an accessory',
car_model: 'xv',
model_year: '2013',
images: be_kind_of(Array),
category_id: accessory.category.to_param,
dealer_id: accessory.dealer.to_param,
url: be_kind_of(String)
})
end
end
And the one for categories does not:
describe "GET #index", get: true do
let (:category) { FactoryGirl.create(:category) }
before do
get :index, params: {}, session: valid_session, format: :json
end
it "responses with OK status" do
expect(response).to have_http_status(:success)
end
it "responses with a non-empty Array" do
expect(json_body).to be_kind_of(Array)
expect(json_body.length).to eq(1)
end
it "responses with JSON containing category" do
expect(response.body).to be_json
expect(json_body.first.with_indifferent_access).to match({
id: category.to_param,
name: 'Test category',
image: be_kind_of(String),
url: be_kind_of(String)
})
end
end
As you can see the logic is the same: issuing a request in the before hook and using let to set the object.
Another strange thing is that GET show test for categories with the same logic works perfectly.
In these questions (1, 2) they say it might be due to DatabaseCleaner strategy and one should use truncation instead of transaction strategy. Which I do since Mongoid allows only truncation. And I am also not using JavaScript-enabled tests and specifically told rspec to use_transactional_fixtures = false
RSpec config for FactoryGirl and DatabaseCleaner:
RSpec.configure do |config|
config.include FactoryGirl::Syntax::Methods
config.before(:suite) do
DatabaseCleaner.strategy = :truncation
DatabaseCleaner.clean_with(:truncation)
end
config.before(:each, :js => true) do
DatabaseCleaner.strategy = :truncation
end
config.before(:each) do
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
end
I am able to make these tests pass by issuing request and creating an object in each example instead of using before and let. But I think it should work with them.
Controller index methods are default:
def index
#thing = Thing.all
end
Do you have any thoughts on this strange behaviour?

Please try let! instead of let.
Note that let is lazy-evaluated. Category data is generated when you call category.to_param. It does not exist in the before block.
See also https://relishapp.com/rspec/rspec-core/v/3-5/docs/helper-methods/let-and-let

Related

Rspec variable created in before block is deleted by the time feature spec reaches POSTed controller action

I have a truncation database cleaning strategy, so not sure why else this is happening. Basically just doing a single feature spec to test that an order gets created appropriately.
require 'rails_helper'
describe "create successfully", type: :feature, js: true do
before do
#site = create(:site)
visit "/orders"
.... # various actions to build an order using the page's form
puts ">>>>>"
puts "site in before action: #{Site.all.size}"
find("#checkoutModal #submit").click()
sleep(1)
end
it "should create" do
expect(Order.all.size).to equal(1)
end
end
# controller action that #submit POSTs to
def create
puts ">>>>>"
puts "site in controller create: #{Site.all.size}"
#order = Order.new(order_params)
#order.save if #order.valid?
end
# puts output:
>>>>>
site in before action: 1
>>>>>
site in controller create: 0
The spec fails because #order creation depends on a #site. Any thoughts on why the #site is being destroyed? Again I do have a truncation set up correctly:
# rails_helper.rb
Rspec.configure do |config|
config.use_transactional_fixtures = false
config.before(:each) do
DatabaseCleaner.strategy = :transaction
end
config.before(:each, js: true) do
DatabaseCleaner.strategy = :truncation
end
config.before(:each, truncate: true) do
DatabaseCleaner.strategy = :truncation
end
config.before(:each) do
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
end
A better way to test this altogether is to use the change matcher:
Rspec.feature "Creating orders", js: true do
let!(:site ) { create(:site) }
def fill_in_and_submit_form
visit "/orders"
# ...
fill_in "something", with: attributes[:something]
find("#checkoutModal #submit").click()
end
context "with valid attributes" do
let(:attributes){ attributes_for(:order) }
it "creates an order" do
expect do
fill_in_and_submit_form
end.to change(Order, :count).by(1)
end
end
context "with invalid attributes" do
let(:attributes) do
{} # should be a hash with invalid attributes
end
it "does not create an order" do
expect do
fill_in_and_submit_form
end.to_not change(Order, :count)
end
end
end
This creates a count query before and after the block is evaluated. One thing you should bear in mind with .size is that will return the length of the collection if it already has been loaded. Which is not a good thing here as you need a db count.
Naming your top level feature description "create successfully" is not a good idea. It does not describe what you are testing it would require you to creates two files to test for success and failure.
That controller is also just plain wrong.
def create
#order = Order.new(order_params)
if #order.save
redirect_to #order
else
render :new
end
end
#order.save if #order.valid? is silly. .save will validate the record and persist it if its valid. You really just want to check the return value of save to see if the record has actually been saved to the database. If orders is a nested record it should actually look like this as well:
def create
#site = Site.find(params[:site_id]) # you're not passing it through a hidden input are you?
#order = #site.orders.new(order_params)
if #order.save
redirect_to #order
else
render :new
end
end

RSpec tests pass in isolation, but fail when run together

In my code_controller_spec.rb my tests pass when I just run the tests in code_controller_spec.rb. However, when I run the suite, only the create, edit, and update tests fail within code_controller_spec.rb. I'm really not sure where to go from here and I really don't know why my tests fail when running together. I have config.use_transactional_fixtures = false and am using database_cleaner. My app works fine locally with the below actions and I don't receive any errors when creating or updating a post. Let me know if you need additional info.
When I run the suite, the failures I get are:
1) CodesController admin pages #create posts a new code post
Failure/Error:
post :create, params: {
code: {
created_at: Date.today,
title: "Code things",
content: "The content of the code post",
user_id: user.id
}
}
ActionController::UrlGenerationError:
No route matches {:action=>"create", :code=>{:created_at=>Fri, 20 Oct 2017, :title=>"Code things", :content=>"The content of the code post", :user_id=>1}, :controller=>"codes"}
2) CodesController admin pages #edit edits a code post
Failure/Error: get :edit, params: { id: code }
ActionController::UrlGenerationError:
No route matches {:action=>"edit", :controller=>"codes", :id=>#<Code id: 1, title: "Code post title", content: "Coding speak that not everyone can understand...", created_at: "2017-10-20 00:00:00", updated_at: "2017-10-21 06:05:27", user_id: 2>}
3) CodesController admin pages #update updates a code post
Failure/Error: put :update, params: { id: code, code: code2 }
ActionController::UrlGenerationError:
No route matches {:action=>"update", :code=>{:title=>"Updated title", :content=>"Updated content"}, :controller=>"codes", :id=>#<Code id: 1, title: "Code post title", content: "Coding speak that not everyone can understand...", created_at: "2017-10-20 00:00:00", updated_at: "2017-10-21 06:05:27", user_id: 2>}
My spec/controllers/user/codes_controller_spec.rb
RSpec.describe User::CodesController, type: :controller do
let!(:user) { User.create(email: "user#example.com", password: "password") }
let!(:code) { FactoryGirl.create(:code) }
before do
sign_in_as user
expect(response).to have_http_status(:success)
end
describe "admin pages" do
render_views
context "#create" do
it "posts a new code post" do
post :create, params: {
code: {
created_at: Date.today,
title: "Code things",
content: "The content of the code post",
user_id: user.id
}
}
expect(response).to redirect_to user_codes_path
expect(flash[:success]).to eq "Post created successfully."
end
end
context "#edit" do
it "edits a code post" do
get :edit, params: { id: code }
expect(response).to render_template :edit
end
end
context "#update" do
let(:code2) do
{ title: "Updated title", content: "Updated content" }
end
it "updates a code post" do
put :update, params: { id: code, code: code2 }
code.reload
expect(response).to redirect_to user_code_path(code)
expect(code.title).to eq code2[:title]
expect(code.content).to eq code2[:content]
expect(flash[:success]).to eq "Post updated successfully."
end
end
end
end
My spec/factories/post_factories.rb
FactoryGirl.define do
factory :code do
created_at Date.today
title "Code post title"
content "Coding speak that not everyone can understand..."
association :user, factory: :user
end
factory :life do
created_at Date.today
title "Life post title"
content "The world moves in mysterious ways; whether we want it to..."
association :user, factory: :user
end
end
I run rspec --seed 123 --bisect and then I run the output of that, but all my tests pass. I should mention I'm very new to --bisect.
This is the rspec --seed 123 --bisect output and when I run this all my tests pass.
rspec ./spec/controllers/user/codes_controller_spec.rb[1:1:1:1,1:1:2:1,1:1:3:1,1:1:4:1,1:1:5:1] ./spec/controllers/user/lives_controller_spec.rb[1:1:1,1:1:2] ./spec/features/admin_create_posts_spec.rb[1:1,1:2,1:3,1:4] ./spec/features/admin_edit_post_spec.rb[1:1,1:2,1:3] ./spec/features/clearance/user_signs_out_spec.rb[1:1] ./spec/features/clearance/visitor_resets_password_spec.rb[1:1,1:2,1:3,1:4,1:5,1:6] ./spec/features/clearance/visitor_signs_in_spec.rb[1:1,1:2,1:3,1:4,1:5,1:6,1:7,1:8] ./spec/features/clearance/visitor_signs_up_spec.rb[1:1,1:2,1:3,1:4,1:5,1:6,1:7,1:8] ./spec/features/clearance/visitor_updates_password_spec.rb[1:1,1:2,1:3] ./spec/features/homepage_spec.rb[1:1,1:2] ./spec/features/login_users_spec.rb[1:1,1:2] ./spec/features/logout_user_spec.rb[1:1] ./spec/features/showing_all_posts_spec.rb[1:1,1:2] ./spec/helpers/codes_helper_spec.rb[1:1] ./spec/helpers/lives_helper_spec.rb[1:1] ./spec/helpers/user/codes_helper_spec.rb[1:1] ./spec/helpers/user/lives_helper_spec.rb[1:1] ./spec/helpers/users_helper_spec.rb[1:1] ./spec/helpers/welcome_helper_spec.rb[1:1] ./spec/models/code_spec.rb[1:1] ./spec/models/life_spec.rb[1:1]
My database_cleaner config in rails_helper.rb
require database_cleaner
RSpec.configure do |config|
config.use_transactional_fixtures = false
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
# config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.before(:all) do
FactoryGirl.reload
end
# If you're not using ActiveRecord, or you'd prefer not to run each of your
# examples within a transaction, remove the following line or assign false
# instead of true.
config.before(:suite) do
if config.use_transactional_fixtures?
raise(<<-MSG)
Delete line `config.use_transactional_fixtures = true` from rails_helper.rb (or set it to false) to prevent uncommitted transactions being used in JavaScript-dependent specs.
During testing, the app-under-test that the browser driver connects to uses a different database connection to the database connection used by the spec. The app's database connection would not be able to access uncommitted transaction data setup over the spec's database connection.
MSG
end
DatabaseCleaner.clean_with(:truncation)
end
config.before(:each) do
DatabaseCleaner.strategy = :transaction
end
config.before(:each, type: :feature) do
# :rack_test driver's Rack app under test shares database connection
# with the specs, so continue to use transaction strategy for speed.
driver_shares_db_connection_with_specs = Capybara.current_driver == :rack_test
if !driver_shares_db_connection_with_specs
# Driver is probably for an external browser with an app under test that does *not*
# share a database connection with the specs, so use truncation strategy.
DatabaseCleaner.strategy = :truncation
end
end
# adds this from
# https://stackoverflow.com/questions/37753251/actionmailer-not-delivering-confirmation-emails-in-test-environment-rails-4
config.before(:each, truncation: true) do
Database::Cleaner.strategy = :truncation
end
config.before(:each) do
DatabaseCleaner.start
end
config.append_after(:each) do
DatabaseCleaner.clean
end
end
UPDATED
Output for rails routes
Prefix Verb URI Pattern Controller#Action
root GET / welcome#index
passwords POST /passwords(.:format) clearance/passwords#create
new_password GET /passwords/new(.:format) clearance/passwords#new
session POST /session(.:format) clearance/sessions#create
edit_user_password GET /users/:user_id/password/edit(.:format) clearance/passwords#edit
user_password PATCH /users/:user_id/password(.:format) clearance/passwords#update
PUT /users/:user_id/password(.:format) clearance/passwords#update
POST /users/:user_id/password(.:format) clearance/passwords#create
users POST /users(.:format) clearance/users#create
sign_in GET /sign_in(.:format) clearance/sessions#new
sign_out DELETE /sign_out(.:format) clearance/sessions#destroy
GET /sign_out(.:format) clearance/sessions#destroy
sign_up GET /sign_up(.:format) clearance/users#new
user_lives GET /user/lives(.:format) user/lives#index
POST /user/lives(.:format) user/lives#create
new_user_life GET /user/lives/new(.:format) user/lives#new
edit_user_life GET /user/lives/:id/edit(.:format) user/lives#edit
user_life GET /user/lives/:id(.:format) user/lives#show
PATCH /user/lives/:id(.:format) user/lives#update
PUT /user/lives/:id(.:format) user/lives#update
DELETE /user/lives/:id(.:format) user/lives#destroy
user_codes GET /user/codes(.:format) user/codes#index
POST /user/codes(.:format) user/codes#create
new_user_code GET /user/codes/new(.:format) user/codes#new
edit_user_code GET /user/codes/:id/edit(.:format) user/codes#edit
user_code GET /user/codes/:id(.:format) user/codes#show
PATCH /user/codes/:id(.:format) user/codes#update
PUT /user/codes/:id(.:format) user/codes#update
DELETE /user/codes/:id(.:format) user/codes#destroy
lives GET /lives(.:format) lives#index
life GET /lives/:id(.:format) lives#show
codes GET /codes(.:format) codes#index
code GET /codes/:id(.:format) codes#show
I believe this is an autoload issue. If your ::CodesController gets loaded before the Users::CodesController then your spec for Users::CodesController is actually using ::CodesController and hence can't create the correct routes. When run singly the correct class is autoloaded and everything works correctly. To fix this require the correct class at the beginning of your spec.
# spec/controllers/user/codes_controller_spec.rb
require 'user/codes_controller'
...
and
# spec/controllers/codes_controller_spec.rb
require 'codes_controller'
...

Rails 4 rspec 3 controller test: session helper module not working for before(:all), works for before(:each)

I'm building a toy chat application using Rails 4.2.7, and am writing specs for my controllers using rspec 3.5. My Api::ChatroomsController requires a user to be logged in in order to create a chatroom, so I have created a Api::SessionsHelper module to create sessions from within the Api::ChatroomsController spec.
# app/helpers/api/sessions_helper.rb
module Api::SessionsHelper
def current_user
User.find_by_session_token(session[:session_token])
end
def create_session(user)
session[:session_token] = user.reset_session_token!
end
def destroy_session(user)
current_user.try(:reset_session_token!)
session[:session_token] = nil
end
end
# spec/controllers/api/chatrooms_controller_spec.rb
require 'rails_helper'
include Api::SessionsHelper
RSpec.describe Api::ChatroomsController, type: :controller do
before(:all) do
DatabaseCleaner.clean
User.create!({username: "test_user", password: "asdfasdf"})
end
user = User.find_by_username("test_user")
context "with valid params" do
done = false
# doesn't work if using a before(:all) hook
before(:each) do
until done do
create_session(user)
post :create, chatroom: { name: "chatroom 1" }
done = true
end
end
let(:chatroom) { Chatroom.find_by({name: "chatroom 1"}) }
let(:chatroom_member) { ChatroomMember.find_by({user_id: user.id, chatroom_id: chatroom.id}) }
it "responds with a successful status code" do
expect(response).to have_http_status(200)
end
it "creates a chatroom in the database" do
expect(chatroom).not_to eq(nil)
end
it "adds the chatroom creator to the ChatroomMember table" do
expect(chatroom_member).not_to eq(nil)
end
end
end
I'm using a before(:each) hook with a boolean variable done to achieve the behavior of a before(:all) hook for creating a single session.
If I use a before(:all) hook, I get the error:
NoMethodError: undefined method `session' for nil:NilClass`
I put a debugger in the create_session method of the Api::SessionsHelper module to check self.class and in both cases, when I use before(:each) and when I use before(:all), the class is:
RSpec::ExampleGroups::ApiChatroomsController::WithValidParams
However when using the before(:each) hook, session is {}, while in the before(:all) hook, session gives the NoMethodError above.
Anybody know what causes this error?
You need to include the helper in the test block:
RSpec.describe Api::ChatroomsController, type: :controller do
include Api::SessionsHelper
end
You can also avoid duplication by including common spec helpers in spec/rails_helper.rb
RSpec.configure do |config|
# ...
config.include Api::SessionsHelper, type: :controller
end
This is also where you should put the database_cleaner config. You should use to clean between every spec not just before all as that will lead to test ordering issues and flapping tests.
require 'capybara/rspec'
#...
RSpec.configure do |config|
config.include Api::SessionsHelper, type: :controller
config.use_transactional_fixtures = false
config.before(:suite) do
if config.use_transactional_fixtures?
raise(<<-MSG)
Delete line `config.use_transactional_fixtures = true` from rails_helper.rb
(or set it to false) to prevent uncommitted transactions being used in
JavaScript-dependent specs.
During testing, the app-under-test that the browser driver connects to
uses a different database connection to the database connection used by
the spec. The app's database connection would not be able to access
uncommitted transaction data setup over the spec's database connection.
MSG
end
DatabaseCleaner.clean_with(:truncation)
end
config.before(:each) do
DatabaseCleaner.strategy = :transaction
end
config.before(:each, type: :feature) do
# :rack_test driver's Rack app under test shares database connection
# with the specs, so continue to use transaction strategy for speed.
driver_shares_db_connection_with_specs = Capybara.current_driver == :rack_test
if !driver_shares_db_connection_with_specs
# Driver is probably for an external browser with an app
# under test that does *not* share a database connection with the
# specs, so use truncation strategy.
DatabaseCleaner.strategy = :truncation
end
end
config.before(:each) do
DatabaseCleaner.start
end
config.append_after(:each) do
DatabaseCleaner.clean
end
end

Objected created in test database is not found by controller

This test clicks a link that hits show_fretboard in static_pages_controller.rb:
describe "StaticPages" do
subject { page }
let!(:key) { FactoryGirl.create(:key, user: user, name: 'C') }
.
.
.
describe "the fretBoard" do
before { visit root_path }
it 'should appear when clicked', js: true do
puts key.name
puts key.id
puts Key.find_by(name: 'C').id
click_link 'fretBoard-side-link'
page.should have_selector("#fretboard-key-header", :text => "Showing Notes in the Key of")
end
end
.
.
.
end
The test's failure shows this:
Run options: include {:line_numbers=>[141]}
C
9
9
An error occurred in an after hook
NoMethodError: undefined method `id' for nil:NilClass
occurred at /Users/user_name/rails_projects/my_project/app/controllers/static_pages_controller.rb:69:in `show_fretboard'
The key exists as it is outputting the key's name and ID. When static_pages_controller.rb tries to find the key, as seen here:
def show_fretboard
#key = params[:key] || Key.find_by(name: 'C').id
This error is thrown:
undefined method `id' for nil:NilClass
Why is this query not finding the Key object with name 'C'?
EDIT: This post (http://justinleitgeb.com/rails/common-rails-testing-mistakes/) says that:
Javascript-enabled tests in Capybara run in another thread
Database elements (from factories, etc) you create in the test example won’t be visible in the server thread
It looks like this is my issue, so my question becomes, how can I use JS tests to test my ajax calls while still querying the database.
EDIT: Database cleaner setup:
RSpec.configure do |config|
puts 'in database_cleaner.rb'
config.before(:suite) do
DatabaseCleaner.clean_with(:truncation)
end
config.before(:each) do
DatabaseCleaner.strategy = :transaction
end
config.before(:each, :js => true) do
DatabaseCleaner.strategy = :truncation
end
config.before(:each) do
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
# source: http://devblog.avdi.org/2012/08/31/configuring-database_cleaner-with-rails-rspec-capybara-and-selenium/
end

Testing after_commit with RSpec and mocking

I have a model Lead and a callback: after_commit :create, :send_to_SPL
I am using Rails-4.1.0, ruby-2.1.1, RSpec.
1) This spec is not passing:
context 'callbacks' do
it 'shall call \'send_to_SPL\' after create' do
expect(lead).to receive(:send_to_SPL)
lead = Lead.create(init_hash)
p lead.new_record? # => false
end
end
2) This spec is not passing too:
context 'callbacks' do
it 'shall call \'send_to_SPL\' after create' do
expect(ActiveSupport::Callbacks::Callback).to receive(:build)
lead = Lead.create(init_hash)
end
end
3) This one is passing, but I think it is not testing after_commit callback:
context 'callbacks' do
it 'shall call \'send_to_SPL\' after create' do
expect(lead).to receive(:send_to_SPL)
lead.send(:send_to_SPL)
end
end
What is the best way to test after_commit callbacks in Rails?
I thought Mihail Davydenkov's comment deserved to be an answer:
You can also use subject.run_callbacks(:commit).
Also note that this issue (commit callbacks not getting called in transactional tests) should be fixed in rails 5.0+ so you may wish to make a note to remove any workarounds you may use in the meantime when you upgrade. See: https://github.com/rails/rails/pull/18458
Try to use test_after_commit gem
or add following code in spec/support/helpers/test_after_commit.rb - Gist
I'm using DatabaseCleaner, with a configuration where I can easily switch between transaction and truncation, where the former is preferred, because of speed, but where the latter can be used for testing callbacks.
RSpec before and after handlers work with scopes, so if you want to make truncation a scope, define a before handler;
config.before(:each, truncate: true) do
DatabaseCleaner.strategy = :truncation
end
And now to use this configuration for a describe, context or it block, you should declare it like:
describe "callbacks", truncate: true do
# all specs within this block will be using the truncation strategy
describe "#save" do
it "should trigger my callback" do
expect(lead).to receive(:send_to_SPL)
lead = Lead.create(init_hash)
end
end
end
Complete hook configuration: (store in spec/support/database_cleaner.rb)
RSpec.configure do |config|
config.before(:suite) do
DatabaseCleaner.clean_with(:truncation)
end
config.before(:each) do
DatabaseCleaner.strategy = :transaction
end
config.before(:each, truncate: true) do
DatabaseCleaner.strategy = :truncation
end
config.before(:each) do
DatabaseCleaner.start
end
config.append_after(:each) do
DatabaseCleaner.clean
end
end
Update for Rails5.
Callback handling has indeed been fixed, but you may still need to use #reload liberally.
An example:
Given a model that defines an after-create callback like so:
after_create_commit { assign_some_association }
You can spec this behavior with:
describe "callbacks" do
describe "assigning_some_association" do
subject(:saving) { record.save!; record.reload } # reload here is important
let(:record) { build(:record) }
it "assigns some association after commit" do
expect{ saving }.to(
change{ record.some_association_id }.from(nil).to(anything)
)
end
end
end
I use something like this
describe 'some method on record' do
let(:record) { create(:some_record) }
let(:update_block) { ->(record) { record.save! } } # define an labmda that will be called in a transaction block
let(:result_method) { :some_method } # define a method to be called
let(:result) do
record.class_eval <<~EVAL, __FILE__, __LINE__ + 1
after_commit :_record_result
def _record_result
#_result = public_send(:#{result_method})
end
EVAL
record.transaction do
update_block.call(record)
end
record.instance_variable_get(:'#_result')
end
before do
# apply changes to record
end
it 'returns the correct result' do
expect(result).to eq(some_value)
end
end

Resources