【问题标题】:Rspec2 nested resources broken redirectRspec2 嵌套资源中断重定向
【发布时间】:2013-02-05 15:15:19
【问题描述】:

我正在尝试为我的嵌套 autolinks_controller 编写 Rspec 测试。但是,我的创建操作后的重定向被破坏。成功创建自动链接后,我希望重定向到特定网站中的该自动链接(因此,website_autolink_path)。我的控制器规格如下所示:

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

我的自动链接控制器:

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." }

在我的本地主机中一切正常,但是为这个嵌套路由编写实际测试让我很困扰。

【问题讨论】:

    标签: ruby-on-rails-3 rspec2 rspec-rails


    【解决方案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
    

    我只需要分配我的自动链接才能重定向到嵌套路径。没有它,我的自动链接的 id 就找不到了。

    【讨论】:

      猜你喜欢
      • 2016-05-22
      • 1970-01-01
      • 2013-06-17
      • 1970-01-01
      • 2013-06-25
      • 2020-06-23
      • 2011-01-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多