Search code examples
ruby-on-railsruby-on-rails-4rspec-railsairborne

API test writes in dev db but reads in test db


I'm trying to test my api with airborne

The call to the api works and create a User, but in the dev DB : there is no log of the creation and i find it using rails c.

But when i do User.find in the test, it searches the test db : there are logs and it doesn't find the user.

Here's my test :

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

Here's the method i'm trying to test :

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

Here's 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

Here's 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

And finally, here's the log :

$ 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)>'

I'm fairly new to rails and i don't know where the problem might come from.

Thanks for your help :)


Solution

  • In my test, i POST to http://myapp.dv/api/v1/register which is a Pow! url.

    Pow! configuration was the default one, which points to the DEV env.

    So my test was in the right env, not the call to the api.

    I used powder to switch envs now and it works.

    ps : i also replaced

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

    with

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