2013-02-05 73 views
0

我想爲我的嵌套autolinks_controller編寫一個Rspec測試。但是,創建操作後的重定向被打破。成功創建自動鏈接後,我希望將其重定向到特定網站中的自動鏈接(因此,即website_autolink_path)。我的控制器的規格如下所示:Rspec2嵌套資源打破重定向

describe "POST create when params[:website_id] are present" do 
    before(:each) do 
    @website = create(:website) 
    @autolink = attributes_for(:website_autolink, website_id: @website.id) 
    end 

    context "with valid attributes and params[:website_id] are present" do 
     it "saved the autolink in the database" do 
      expect{ 
       post :create, website_id: @website, autolink: attributes_for(:website_autolink) 
      }.to change(Autolink, :count).by(1) 
     end 

     it "redirects to the 'index' page" do 
      post :create, website_autolink: @autolink, website_id: @website 
      response.should redirect_to website_autolink_path 
     end 
    end 
end 

此行不工作:

response.should redirect_to website_autolink_path 

給我的錯誤信息:

ActionController::RoutingError: 
    No route matches {:action=>"show", :controller=>"autolinks"} 

我的工廠是這樣的:

自動鏈接:

FactoryGirl.define do 
    factory :website_autolink do 
    name "MyName" 
    url "http://www.myurl.nl" 
    association :website 
    end 
end 

網站:

FactoryGirl.define do 
    factory :website do 
    name "Test" 
    domain "http://www.test.nl" 
    end 
end 

我AutolinkController:

def create 
    if params[:website_id].present? 
     @website = Website.find(params[:website_id]) 
     @autolink = @website.autolinks.create(params[:autolink]) 
    else 
     @autolink = Autolink.new(params[:autolink]) 
    end 

    respond_to do |format| 
     if @autolink.save 
      if params[:website_id].present? 
       format.html { redirect_to [@website, @autolink], notice: "Autolink is met succes aangemaakt." } 
      else 
       format.html { redirect_to autolinks_path, notice: "Autolink is met succes aangemaakt." } 
      end 
      format.json { head :no_content } 
     else 
      format.html { render action: "new" } 
      format.json { render json: @autolink.errors, status: :unprocessable_entity } 
     end 
    end 
end 

在我的控制器,下面一行是一個我想用Rspec的模擬:

format.html { redirect_to [@website, @autolink], notice: "Autolink is met succes aangemaakt." } 

在我的本地主機這一切都工作,但寫這個嵌套路線的實際測試困擾我。

回答

1

我剛剛爲我的問題找到了解決方案。我爲創建動作控制器的規格現在看起來是這樣的:

describe "POST create when params[:website_id] are present" do 
    context "with valid attributes and params[:website_id] are present" do 
    before(:each) do 
     @website = create(:website) 
     @autolink = attributes_for(:website_autolink, website: @website) 
     end 

     it "saved the autolink in the database" do 
      expect{ 
       post :create, autolink: @autolink, website_id: @website.id 
      }.to change(Autolink, :count).by(1) 
     end 

     it "redirects to the 'index' page" do 
      post :create, autolink: @autolink, website_id: @website.id 
      response.should redirect_to website_autolink_path(@website, assigns(:autolink)) 
     end 
    end 
end 

我剛分配我的自動連接,以redirect_to的嵌套路徑。沒有它,我的自動鏈接的id不能被發現。