【问题标题】:Get HTTP response Using Shoulda Ruby on Rails使用 Shoulda Ruby on Rails 获取 HTTP 响应
【发布时间】:2010-09-03 02:02:03
【问题描述】:

我正在从 rspec 迁移到 shoulda,但我似乎无法访问 http 响应。有人能指出我做错了什么吗?

  context "doing somethin" do
      setup do
        get :index
      end
      @response.body
      should respond_with :success
  end

当我运行它时,我收到一条错误消息,指出 @response 是一个 nill 对象。

【问题讨论】:

    标签: ruby-on-rails ruby testing shoulda


    【解决方案1】:

    如果你想访问响应,你应该首先像这样包装成一个“应该”:

    context "doing somethin" do
      setup do
        get :index
      end
    
      should "i access..." do
       assert response.status, 200
      end
    end
    

    这就像你尝试在测试之外使用响应,每个应该代表一个测试用例,而上下文就像 rspec 中的 before(:each)。

    【讨论】:

      【解决方案2】:

      我相信应该的语法是:

      should_respond_with :success

      代替:

      应该用 :success 响应

      【讨论】:

      • should_respond_with :success 宏在 v2.11.x 中被弃用,支持使用 should respond_with(:success)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-27
      • 1970-01-01
      • 1970-01-01
      • 2012-01-16
      • 1970-01-01
      相关资源
      最近更新 更多