“没有路线匹配”与rspec

时间:2013-12-03 09:47:43

标签: ruby-on-rails-3 rspec

我已经运行了一个控制器测试集,其中三个测试成功,三个失败并出现相同类型的错误。

对于editupdatedestroy操作的测试,我收到相关错误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

1 个答案:

答案 0 :(得分:1)

我在这里看到两个错误

  • 你没有使用正确的动词进行销毁和更新,你应该使用'delete'进行销毁,'put'进行更新
  • 您没有为这些操作提供“ID”,您应该使用get :edit, id: 1put :update, id: 1 ...

尝试运行佣金路线以查看您的确切路线

PS:我认为你也会因show动作而得到同样的错误。如果您不需要该操作,请将其作为except: :show传递到routes.rb

上的资源中