我有一個名爲Xpmodule
的Rails型號,並帶有相應的控制器XpmoduleController
。測試帶有RSpec和不同路由名稱的Ruby on Rails控制器
class XpmoduleController < ApplicationController
def index
@xpmodule = Xpmodule.find(params[:module_id])
end
def subscribe
flash[:notice] = "You are now subscribed to #{params[:subscription][:title]}"
redirect_to :action => :index
end
end
最初的意圖是來命名其原因很明顯不起作用模型Module
。不過,我還是希望有網址的樣子/module/4711/
因此,我已將此添加到我的routes.rb
:
map.connect '/module/:module_id', :controller => 'xpmodule', :action => 'index'
map.connect '/module/:module_id/subscribe', :controller => 'xpmodule',
:action => 'subscribe'
現在我想使用RSpec來測試這個控制器:
describe XpmoduleController do
fixtures :xpmodules
context "index" do
it "should assign the current xpmodule" do
xpm = mock_model(Xpmodule)
Xpmodule.should_receive(:find).and_return(xpm)
get "index"
assigns[:xpmodule].should be_an_instance_of(Xpmodule)
end
end
end
對此我得到No route matches {:action=>"index", :controller=>"xpmodule"}
。當然這是正確的,但我不想僅僅爲了測試目的而添加這條路線。有沒有辦法告訴Rspec在get
中調用不同的URL?
看到我的答案,我只是沒有調用正確的參數的行動。 – jhwist 2010-03-26 10:22:10