Reputation: 136
I have controller Categories which have action create:
def create
@category = Category.new(category_params)
respond_to do |format|
if @category.save
format.html { redirect_to @category, notice: 'Category was successfully created.' }
format.json { render :show, status: :created, location: @category }
else
format.html { render :new }
format.json { render json: @category.errors, status: :unprocessable_entity }
end
end
end
def category_params
params.require(:category).permit(:name, :user_id)
end
I need to test it by using RSpec. I wrote test:
before(:each) do
@user = create(:user)
sign_in @user
end
describe 'POST #create' do
it 'creates category' do
expect do
post :create, params: {
category: attributes_for(:category, user_id: @user.id)
}
end.to change { Category.count }.by(1) and
redirect_to Category.last && have_http_status(200) and
render_template('show')
end
end
When I run
$ bundle exec rspec
it output
Finished in 0.55119 seconds (files took 7.36 seconds to load) 2 examples, 0 failures
But when I come to
rails c test
and try to output Cateroy.all or Category.first it show me that database is empty. Also I tried to check database via pgadmin and it is clear.
But I don't understand why it is clear after it successfull pass tests. I don't use gem 'database_cleaner' Also I can provide my last test logs:
Completed 200 OK in 139ms (Views: 44.3ms | ActiveRecord: 4.5ms)
[1m[35m (0.1ms)[0m [1m[31mROLLBACK[0m
[1m[35m (0.1ms)[0m [1m[35mBEGIN[0m
[1m[35m (0.1ms)[0m [1m[35mSAVEPOINT active_record_1[0m
[1m[36mUser Exists (0.2ms)[0m [1m[34mSELECT 1 AS one FROM "users" WHERE "users"."email" = $1 LIMIT $2[0m [["email", "[email protected]"], ["LIMIT", 1]]
[1m[35mSQL (0.3ms)[0m [1m[32mINSERT INTO "users" ("email", "encrypted_password", "confirmed_at", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id"[0m [["email", "[email protected]"], ["encrypted_password", "$2a$04$8uzV2Wd4I7iOHvywVl3CUOK..eSLgSOwjYL31Fo6bu6wR.k4YPDVS"], ["confirmed_at", "2018-01-11 00:00:00"], ["created_at", "2018-01-11 16:35:24.255470"], ["updated_at", "2018-01-11 16:35:24.255470"]]
[1m[35m (0.1ms)[0m [1m[35mRELEASE SAVEPOINT active_record_1[0m
[1m[35m (0.3ms)[0m [1m[34mSELECT COUNT(*) FROM "categories"[0m
Processing by CategoriesController#create as HTML
Parameters: {"category"=>{"name"=>"Amani Rogahn", "user_id"=>"4"}}
[1m[36mUser Load (0.3ms)[0m [1m[34mSELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2[0m [["id", 4], ["LIMIT", 1]]
[1m[35m (0.1ms)[0m [1m[35mSAVEPOINT active_record_1[0m
[1m[36mCategory Exists (0.3ms)[0m [1m[34mSELECT 1 AS one FROM "categories" WHERE ("categories"."id" IS NOT NULL) AND "categories"."slug" = $1 LIMIT $2[0m [["slug", "amani-rogahn"], ["LIMIT", 1]]
[1m[36mUser Load (0.2ms)[0m [1m[34mSELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2[0m [["id", 4], ["LIMIT", 1]]
[1m[35mSQL (0.3ms)[0m [1m[32mINSERT INTO "categories" ("name", "user_id", "created_at", "updated_at", "slug") VALUES ($1, $2, $3, $4, $5) RETURNING "id"[0m [["name", "Amani Rogahn"], ["user_id", 4], ["created_at", "2018-01-11 16:35:24.564081"], ["updated_at", "2018-01-11 16:35:24.564081"], ["slug", "amani-rogahn"]]
[1m[35m (0.1ms)[0m [1m[35mRELEASE SAVEPOINT active_record_1[0m
Redirected to http://test.host/en/categories/amani-rogahn
Completed 302 Found in 9ms (ActiveRecord: 1.4ms)
[1m[35m (0.2ms)[0m [1m[34mSELECT COUNT(*) FROM "categories"[0m
[1m[36mCategory Load (0.2ms)[0m [1m[34mSELECT "categories".* FROM "categories" ORDER BY "categories"."id" DESC LIMIT $1[0m [["LIMIT", 1]]
[1m[35m (0.1ms)[0m [1m[31mROLLBACK[0m
Here is my rails_helper.rb
require 'support/factory_bot'
require 'spec_helper'
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
abort("The Rails environment is running in production mode!") if Rails.env.production?
require 'rspec/rails'
ActiveRecord::Migration.maintain_test_schema!
RSpec.configure do |config|
config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.use_transactional_fixtures = true
config.infer_spec_type_from_file_location!
config.filter_rails_from_backtrace!
config.include Devise::Test::ControllerHelpers, type: :controller
end
Here is my spec_helper.rb
RSpec.configure do |config|
config.expect_with :rspec do |expectations|
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
end
config.mock_with :rspec do |mocks|
end
config.shared_context_metadata_behavior = :apply_to_host_groups
=begin
config.filter_run_when_matching :focus
config.example_status_persistence_file_path = "spec/examples.txt"
config.disable_monkey_patching!
if config.files_to_run.one?
config.default_formatter = "doc"
end
config.profile_examples = 10
config.order = :random
Kernel.srand config.seed
=end
end
Please help me to understand why this test not affect on test database and how to change this situation.
Upvotes: 2
Views: 1088
Reputation: 15987
It's because you have this line:
config.use_transactional_fixtures = true
The name of this setting is a bit misleading. What it really means in Rails is "run every test method within a transaction." In the context of rspec-rails, it means "run every example within a transaction."
The idea is to start each example with a clean database, create whatever data is necessary for that example, and then remove that data by simply rolling back the transaction at the end of the example.
See: https://relishapp.com/rspec/rspec-rails/docs/transactions
Upvotes: 3