首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >request.fullpath与RSpec

request.fullpath与RSpec
EN

Stack Overflow用户
提问于 2011-06-03 08:21:20
回答 1查看 2.2K关注 0票数 1

在我的应用程序控制器中,有两个方法定义如下:

代码语言:javascript
复制
def store_location
  session[:return_to] = request.fullpath
end

def redirect_back_or_default(default)

  # make sure this method doesn't redirect back to the current page
  if session[:return_to] == request.fullpath
    redirect_to default
  else
    redirect_to(session[:return_to] || default)
  end

  session[:return_to] = nil
end

为了测试这些方法,我需要找到在RSpec中设置RSpec的方法。有人知道我怎么能做到这一点吗?

更新

在测试这些方法时,我使用的是一个共享示例组,如下所示:

代码语言:javascript
复制
shared_examples_for "redirect back or default" do
  it "should redirect" do
    request
    response.should be_redirect
  end

  describe "when the user has a back page" do
    it "should redirect to back"
  end

  describe "when the user does not have a back page" do
    it "should redirect to default" do
      request
      response.should redirect_to(default_path)
    end
  end
end

当包含共享示例组时,我执行如下操作:

代码语言:javascript
复制
before(:each) do
  def request
    post :create, :user => @attr
  end
  def default_path
    :root
  end
end

include_examples "redirect back or default"

因此,当一个方法使用redirect_back_or_default时,我只需将上面的代码添加到它的测试中,我就是这样的done.In,我仍然可以对redirect_back_or_default进行具体的测试,而不必针对实现进行测试,这似乎是对我来说做BDD的更好的方法。

EN

回答 1

Stack Overflow用户

发布于 2011-06-07 04:07:49

您可以访问可以直接设置的@request对象,如下所示:

代码语言:javascript
复制
@request.fullpath = "/my/path"

但是,当您在控制器规范中进行实际的get/post/put/delete时,我的猜测将被覆盖。

你想直接设置完全路径而不是知道完全路径的任何原因都是简单的,比如"/mock"?

代码语言:javascript
复制
class MocksController < ApplicationController
  def show
  end
end

describe MocksController do
  before do
    MyApp::Application.routes.draw do
      resource :mock, :only => [:show]
    end
  end

  after do
    Rails.application.reload_routes!
  end

  it "something" do
    get :show
    should # something
  end
end
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6224675

复制
相关文章

相似问题

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