首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Rspec2嵌套资源中断重定向

Rspec2嵌套资源中断重定向
EN

Stack Overflow用户
提问于 2013-02-05 23:15:19
回答 1查看 86关注 0票数 0

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

代码语言:javascript
复制
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

这条线路不工作:

代码语言:javascript
复制
response.should redirect_to website_autolink_path

给我错误消息:

代码语言:javascript
复制
ActionController::RoutingError:
   No route matches {:action=>"show", :controller=>"autolinks"}

我的工厂是这样的:

自动链接:

代码语言:javascript
复制
FactoryGirl.define do
  factory :website_autolink do
    name "MyName"
    url "http://www.myurl.nl"
    association :website
  end 
end

网站:

代码语言:javascript
复制
FactoryGirl.define do
  factory :website do
    name "Test"
    domain "http://www.test.nl"
  end 
end

我的AutolinkController:

代码语言:javascript
复制
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来模拟的:

代码语言:javascript
复制
format.html { redirect_to [@website, @autolink], notice: "Autolink is met succes aangemaakt." }

在我的localhost中,一切正常,但是为这个嵌套的路由编写实际的测试给我带来了麻烦。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-02-06 17:39:54

我刚刚找到了我的问题的解决方案。我的create操作的控制器规范现在看起来如下所示:

代码语言:javascript
复制
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就找不到了。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14710751

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档