没有路由匹配{:controller =>"stocks",:action =>"create"} RSpec Rails 3

and*_*s32 15 ruby rspec ruby-on-rails

我不明白为什么我在运行RSpec时收到此错误消息:

Failure/Error: post :create
 ActionController::RoutingError:
   No route matches {:controller=>"stocks", :action=>"create"}
Run Code Online (Sandbox Code Playgroud)

存在控制器库存,操作创建存在,它应该使用的路由是:

match 'stocks/:user_id' => 'stocks#create', :via => :post, :as => :query
Run Code Online (Sandbox Code Playgroud)

路线文件:

FruthScreener::Application.routes.draw do
root :to => 'stocks#index' # add user_id
match 'stocks/:user_id' => 'stocks#create', :via => :post, :as => :query
match 'stocks/:user_id' => 'stocks#destroy', :via => :delete, :as => :reset
match 'stocks/:user_id/update' => "stocks#update_index", :via => :post
Run Code Online (Sandbox Code Playgroud)

库存控制器:(是的,这将被干掉)

class StocksController < ApplicationController
def index
    @user = User.create
    @user_id_num = @user.id
    @user.stocks = Stock.all
    @user.save
    selected_user_stocks = (UserStock.where("user_id = #{@user_id_num} AND selected = true")).take(25)
    @stocks = UserStock.convert_to_stocks(selected_user_stocks)
    @pages = Pagination.determine_num_pages(@user.stocks) 
end

def update_index
    @user_id_num = params[:user_id]
    @user = User.find(@user_id_num)
    selected_user_stocks = UserStock.where("user_id = #{@user_id_num} AND selected = true")
    @stocks = UserStock.convert_to_stocks(selected_user_stocks)
    @pages = Pagination.determine_num_pages(@stocks)
    @stocks = Stock.display(@stocks, params[:pageNumber].to_i)
    render partial: 'stock_data'
end

def create
    UserStock.eliminate_stocks(params) # thinking it's like "creating" a new batch of stocks
    @user_id_num = params[:user_id]
    @user = User.find(@user_id_num)
    selected_user_stocks = UserStock.where("user_id = #{@user_id_num} AND selected = true")
    @stocks = UserStock.convert_to_stocks(selected_user_stocks)
    @pages = Pagination.determine_num_pages(@stocks) 
    @stocks = Stock.display(@stocks, 1)
    render partial: 'stock_data'
end

def destroy
    stocks_to_reset = UserStock.where("user_id = #{params[:user_id]}")
    stocks_to_reset.each do |user_stock|
        user_stock.selected = true
        user_stock.save
    end
    @user_id_num = params[:user_id]
    @user = User.find(@user_id_num)
     selected_user_stocks = UserStock.where("user_id = #{@user_id_num} AND selected = true")
    @stocks = UserStock.convert_to_stocks(selected_user_stocks)
    @pages = Pagination.determine_num_pages(@stocks) 
    @stocks = Stock.display(@stocks, 1)
    render partial: 'stock_data'
end


end
Run Code Online (Sandbox Code Playgroud)

库存控制器规格:(底部测试失败)

require 'spec_helper'

RSpec.configure do |config|
  config.infer_spec_type_from_file_location!
end

describe StocksController do

  context "index" do 
    before do 
        @user_count = User.all.count 
        @stock_count = Stock.all.count
        get :index
    end

    it "should create a new user" do
        expect(User.all.count).to eq(@user_count + 1)
    end

    it "should associate all stocks with the user" do
        expect(User.last.stocks.count).to eq(@stock_count)
    end
  end

  context "create" do # investigate why this isn't working...
    it "should render the stock_list partial" do
        user = User.create
        post :create
        response.should render_template(partial: "stock_list")
    end
  end

end
Run Code Online (Sandbox Code Playgroud)

谢谢!!

Tar*_*ast 32

match 'stocks/:user_id' => 'stocks#create', :via => :post, :as => :query
Run Code Online (Sandbox Code Playgroud)

告诉我们它需要:user_id是一个有效的路由.

post :create
Run Code Online (Sandbox Code Playgroud)

没有:user_id ...所以它没有引用有效的路由.我把它换成了

post :create, :user_id => user.id
Run Code Online (Sandbox Code Playgroud)

  • 这会在Rails 5中产生弃用警告:DEPRECATION警告:ActionController :: TestCase HTTP请求方法将在将来的Rails版本中仅接受关键字参数。`现在正确的语法为:`post:create,params:{user_id:user。 id}` (2认同)