我有控制器类别,其中包含动作创建:
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
我需要使用RSpec进行测试。 我写了测试:
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
当我跑步时
$ bundle exec rspec
输出
在0.55119秒内完成(文件加载时间为7.36秒)2 例子,0失败
但是当我来到
rails c test
并尝试输出Cateroy.all或Category.first它向我显示数据库为空。此外,我试图通过pgadmin检查数据库,很明显。
但是我不明白为什么在成功通过测试后它很清楚。 我不使用gem'database_cleaner' 我也可以提供我的最后测试日志:
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", "roxane@bartoletti.org"], ["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", "roxane@bartoletti.org"], ["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
这是我的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
这是我的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
请帮助我理解为什么此测试不会影响测试数据库以及如何改变这种情况。
答案 0 :(得分:3)
这是因为你有这一行:
application/json
此设置的名称有点误导。它真正意味着什么 Rails是"在事务中运行每个测试方法。"在上下文中 rspec-rails表示"运行事务中的每个例子。"
我们的想法是使用干净的数据库启动每个示例,创建 该示例所需的任何数据,然后删除该数据 通过简单回滚示例末尾的事务。
请参阅:https://relishapp.com/rspec/rspec-rails/docs/transactions