Chloe
Chloe

Reputation: 26264

Why is my unit test failing for POST/#create?

I have the following

brokers_controller.rb
class BrokersController < ApplicationController
  before_action :authenticate_broker!
  before_action :set_broker, only: [:show, :edit, :update, :approve, :destroy]

  # GET /brokers
  # GET /brokers.json
  def index
    if current_user && current_user.admin
      @brokers = Broker.all
    else
      @brokers = Broker.where(approved: true)
    end
  end

  # GET /brokers/1
  # GET /brokers/1.json
  def show
  end

  # GET /brokers/new
  def new
    redirect_to brokers_path # brokers can't create brokers
    @broker = Broker.new
  end

  # GET /brokers/1/edit
  def edit
  end

  # POST /brokers
  # POST /brokers.json
  def create
    redirect_to brokers_path
    return
    # @broker = Broker.new(broker_params)
# 
    # respond_to do |format|
      # if @broker.save
        # format.html { redirect_to @broker, notice: 'Broker was successfully created.' }
        # format.json { render :show, status: :created, location: @broker }
      # else
        # format.html { render :new }
        # format.json { render json: @broker.errors, status: :unprocessable_entity }
      # end
    # end
  end
brokers_controller_test.rb
require 'test_helper'

class BrokersControllerTest < ActionDispatch::IntegrationTest
  include Warden::Test::Helpers

  setup do
    @broker = brokers(:one)
    login_as(@broker, :scope => :broker)
  end

  teardown do
    Warden.test_reset! 
  end

  test "should get index" do
    get brokers_url
    assert_response :success
  end

  test "should get new" do
    get new_broker_url
    assert_response :redirect
    assert_redirected_to brokers_url
  end

  test "should create broker" do
    # assert_no_difference('Broker.count') do
      post brokers_url, params: { broker: { email: @broker.email, name: @broker.name } }
    # end

    assert_redirected_to brokers_url
  end

Log

>rails test test/controllers/brokers_controller_test.rb:26
Run options: --seed 52623

# Running:

F

Failure:
BrokersControllerTest#test_should_create_broker [C:/Users/Chloe/workspace/test/controllers/brokers_controller_test.rb:31]:
Expected response to be a redirect to <http://www.example.com/brokers> but was a redirect to <http://www.example.com/>.
Expected "http://www.example.com/brokers" to be === "http://www.example.com/".


bin/rails test test/controllers/brokers_controller_test.rb:26

It keeps redirecting to root, but I can't see why. The "should get index" and "should get new" test work OK.

Upvotes: 0

Views: 70

Answers (1)

Chloe
Chloe

Reputation: 26264

Oh I figured out what it was. I commented this to view it in the browser

  def new
    # redirect_to brokers_path # brokers can't create brokers

It redirected to root like the test with the alert

You are already signed in.

The logs show

Started POST "/brokers" for ::1 at 2017-06-28 14:06:02 -0400
Processing by Devise::RegistrationsController#create as HTML
  Parameters: {"utf8"=>"√", "authenticity_token"=>"d4OoCcx5PmjnwOnpHfFOBvsAzAN52BExoOCstC9u/9sVLaithTbFPSIgCFlqILL5n2xTRYni1ZchB62EPKFVYg==", "broker"=>{"name"=>"XXX", "email"=>"[email protected]"}, "commit"=>"Save"}
  Broker Load (3.0ms)  SELECT  "brokers".* FROM "brokers" WHERE "brokers"."id" = $1 ORDER BY "brokers"."id" ASC LIMIT $2  [["id", 1], ["LIMIT", 1]]
Redirected to http://localhost:3000/
Filter chain halted as :require_no_authentication rendered or redirected
Completed 302 Found in 6ms (ActiveRecord: 3.0ms)

The key was Processing by Devise::RegistrationsController#create. When I look at the routes, I see

>rake routes | grep brokers
...
   new_broker_registration GET    /brokers/sign_up(.:format)                  devise/registrations#new
...
       broker_registration PATCH  /brokers(.:format)                          devise/registrations#update
                           PUT    /brokers(.:format)                          devise/registrations#update
                           DELETE /brokers(.:format)                          devise/registrations#destroy
                           POST   /brokers(.:format)                          devise/registrations#create
...
                   brokers GET    /brokers(.:format)                          brokers#index
                           POST   /brokers(.:format)                          brokers#create

The route for Devise came before the controller.

Rails.application.routes.draw do
  devise_for :brokers
...
  resources :brokers do

So when it POSTs to /brokers, it finds Devise first. I thought Devise would POST to /brokers/sign_up instead.

Upvotes: 1

Related Questions