bdx
bdx

Reputation: 3516

"No route matches" with rspec

I've got a controller test set running where three of the tests succeed and three fail with the same type of error.

For the tests for the edit, update, and destroy actions, I get the associated error saying No route matches {:controller=>"accounts", action=>"edit"}

accounts_controller_spec.rb

describe AccountsController do
before(:each) do
  @account_code = FactoryGirl.create(:account)
end

describe "GET 'index'" do
  it "returns http success" do
    get 'index'
    expect(response).to be_success
  end
end

describe "GET 'new'" do
  it "returns http success" do
    get 'new'
    expect(response).to be_success
  end
end

describe "POST 'create'" do
  it "returns http success" do
    post 'create'
    expect(response).to be_success
  end
end

describe "GET 'edit'" do
  it "returns http success" do
    get 'edit'
    expect(response).to be_success
  end
end

describe "POST 'update'" do
  it "returns http success" do
    post 'update'
    expect(response).to be_success
  end
end

describe "DELETE 'destroy'" do
  it "returns http success" do
    post 'destroy'
    expect(response).to be_success
  end
end
end

accounts_controller.rb

class AccountsController < ApplicationController
  load_and_authorize_resource

  def index
  end

  def new
  end

  def create
    if @account.save
      flash[:success] = "Account created"
      redirect_to :action => :index
    else
      render 'new'
    end
  end

  def update
    if @account.update_attributes(params[:account])
      flash[:success] = "Account Updated"
      redirect_to :action => :index
    else
      render 'edit'
    end
  end

  def edit
  end

  def destroy
    @account.destroy
    flash[:success] = "Account Deleted"
    redirect_to accounts_path
  end
end

routes.rb

resources :account_codes

Upvotes: 0

Views: 2242

Answers (1)

xlembouras
xlembouras

Reputation: 8295

I see two errors here

  • you do not use the correct verbs for destroy and update, you should use 'delete' for destroy and 'put' for update
  • you do not provide an 'id' for these actions, you should use get :edit, id: 1 , put :update, id: 1 ...

try running rake routes to see your exact routes

PS: I think you would get the same error for a show action as well. If you do not need that action, pass it in as except: :show in your resources on routes.rb

Upvotes: 1

Related Questions