API测试在dev db中写入,但在test db中读取

时间:2015-07-08 13:23:06

标签: ruby-on-rails ruby-on-rails-4 rspec-rails airborne

我正试图用airborne

测试我的api

对api的调用工作并创建一个User,但在dev DB中:没有创建日志,我发现它使用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非常陌生,我不知道问题可能来自哪里。

感谢您的帮助:)

1 个答案:

答案 0 :(得分:0)

在我的测试中,我发帖到http://myapp.dv/api/v1/register这是一个Pow!网址。

的Pow!配置是默认配置,指向DEV env。

所以我的测试是在正确的环境中,而不是对api的调用。

我现在使用powder来切换envs并且有效。

ps:我也替换了

expect(User.find_by_email('mail@mail.fr')).to exist

expect(User.find_by_email('mail@mail.fr')).not_to be_nil