【发布时间】:2015-07-08 13:23:06
【问题描述】:
我正在尝试使用 airborne 测试我的 api
对 api 的调用有效并创建了一个用户,但在开发数据库中:没有创建日志,我使用 rails c 找到它。
但是当我在测试中执行 User.find 时,它会搜索测试数据库:有日志但找不到用户。
这是我的测试:
require 'rails_helper'
describe 'register#create' do
it 'should create a new user' do
post '/register',
{
email: 'mail@mail.fr',
password: '12345678',
password_confirmation: '12345678'
}
puts response.body
expect_status 200
expect(User.find_by_email('mail@mail.fr')).to exist
end
end
这是我要测试的方法:
class RegistrationsController < Devise::RegistrationsController
respond_to :json
skip_before_filter :verify_authenticity_token
acts_as_token_authentication_handler_for User
skip_before_filter :authenticate_entity_from_token!, only: [:create]
skip_before_filter :authenticate_entity!, only: [:create]
skip_before_filter :authenticate_scope!
append_before_filter :authenticate_scope!, only: [:destroy]
def create
build_resource(sign_up_params)
if !resource.valid?
status = 422
message = "#{resource.errors.full_messages}"
elsif resource.save!
status = 200
message = "Successfully created new account for email #{sign_up_params[:email]}."
else
clean_up_passwords resource
status = 500
message = "Failed to create new account for email #{sign_up_params[:email]}."
end
respond_to do |format|
format.json { render json: { message: message }, status: status }
end
end
这是 rails_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 'spec_helper'
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!
end
这里是 spec_helper:
ENV['RAILS_ENV'] = 'test'
require 'airborne'
Airborne.configure do |config|
config.base_url = 'http://myapp.dev/api/v1'
end
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|
mocks.verify_partial_doubles = true
end
end
最后,这是日志:
$ rspec
WARN: Unresolved specs during Gem::Specification.reset:
minitest (~> 5.1)
WARN: Clearing out unresolved specs.
Please report a bug if this causes problems.
DEBUG -- : ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
DEBUG -- : (0.1ms) begin transaction
{"message":"Successfully created new account for email mail@mail.fr."}
DEBUG -- : User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."email" = ? LIMIT 1 [["email", "mail@mail.fr"]]
DEBUG -- : (0.1ms) rollback transaction
F
Failures:
1) register#create should create a new user
Failure/Error: expect(User.find_by_email('mail@mail.fr')).to exist
expected nil to exist but it does not respond to either `exist?` or `exists?`
# ./spec/registration_controller_spec.rb:19:in `block (2 levels) in <top (required)>'
我对 Rails 还很陌生,我不知道问题可能出在哪里。
感谢您的帮助:)
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-4 rspec-rails airborne