因此,我目前正在为现有控制器中的一个控制器编写一个测试,这个控制器以前没有.我想测试的是一个重定向,当有人不允许编辑某个内容,而有人允许编辑它时,就会发生重定向.

正在编辑的控制器操作

def edit
  if !@scorecard.reviewed? || admin?
    @company = @scorecard.company
    @custom_css_include = "confirmation_page"
  else
    redirect_to :back
  end
end

因此,如果一个记分卡已经过审核,那么只有管理员可以编辑该分数.

# scorecards
resources :scorecards do
  member do
    get 'report'
  end
  resources :inaccuracy_reports, :only => [:new, :create]
end

最后是测试

  require 'spec_helper'

  describe ScorecardsController do

    describe "GET edit" do
      before(:each) do
        @agency = Factory(:agency)
        @va = Factory(:va_user, :agency => @agency)
        @admin = Factory(:admin)
        @company = Factory(:company)
        @scorecard = Factory(:scorecard, :level => 1, :company => @company, :agency => @agency, :reviewed => true)
        request.env["HTTP_REFERER"] = "/scorecard"
      end

      context "as a admin" do
        before(:each) do
          controller.stub(:current_user).and_return @admin
        end

        it "allows you to edit a reviewed scorecard" do
          get 'edit', :id => @scorecard.id
          response.status.should be(200)
        end
      end

      context "as a va_user" do
        before(:each) do
        controller.stub(:current_user).and_return @va
      end

      it "does not allow you to edit a reviewed scorecard" do
        get 'edit', :id => @scorecard.id
        response.should redirect_to :back
      end
    end
  end
end

因此,va在试图编辑已审核的分数时将被重定向回,而管理员不会.

但在rspec上运行时,我发现

ScorecardsController
  GET edit
    as a admin
      allows you to edit a reviewed scorecard
    as a va_user
      does not allow you to edit a reviewed scorecard (FAILED - 1)

Failures:

  1) ScorecardsController GET edit as a va_user does not allow you to edit a reviewed scorecard
     Failure/Error: response.should redirect_to :back
   Expected response to be a redirect to </scorecard> but was a redirect to <http://test.host/>
     # ./spec/controllers/scorecards_controller_spec.rb:33:in `block (4 levels) in <top (required)>'

Finished in 0.48517 seconds
2 examples, 1 failure

所以我不知道它是否有效,因为我把request.env["HTTP_REFERER"] = "/scorecard"设置为应该是:back的位置.或者,我是不是错过了这个 idea ?看httpstatus个答案,我可以使用300个答案,但我不知道从哪里开始?

任何帮助都会很棒

编辑

我可以这样做来测试

...
response.status.should be(302)

但我是从这question条中得到这个 idea 的,它听起来可能很强大,因为它指定了重定向到的url.

有人做过这样的测试吗?

推荐答案

这条线路有问题

response.should redirect_to :back

逻辑不正确.您应该期望#edit重定向到您之前设置的:back路径,即/scorecard.但你在这里设定了:back.在Rspec的上下文中,:back在每个示例中都应该为空.

要修改,只需将其设置为

response.should redirect_to '/scorecard'

Ruby-on-rails相关问答推荐

使用turbo流的部分渲染在rails 7中不起作用.

仅当未在带有ENV[';Dyno';]的Heroku问题上运行时才初始化Sidekiq Cron

为什么这个rails Collection_select不呈现关联属性?

我使用rbenv安装Ruby,但我在使用";gem";->;错误:执行gem时遇到此错误.(Errno::EACCES)权限被拒绝

轨道强参数,怎么允许空参数?

Rails 创建自定义函数/方法以及存储位置

如何在 YAML 中声明带有单引号和双引号的字符串?

Rails 发现获取 ActiveRecord::RecordNotFound

如何更改 Heroku 应用程序的 DATABASE_URL

在 Nokogiri 中获取属性值以提取链接 URL

Ruby on Rails 的 varchar 迁移问题

Ruby如何写入Tempfile

使用 jQuery 在 Rails 中不显眼的动态表单字段

如何解决弃用警告方法 to_hash 已弃用并将在 Rails 5.1 中删除

在本地 Rails 开发环境中获取真实 IP 地址

如何让 ActiveAdmin 使用强参数?

ActiveRecord 查找现有表索引

Ruby on Rails:如何为 postgresql 编辑 database.yml?

RoR select_tag 默认值和选项

在 Ruby on Rails 中设置日志(log)记录级别