代码之家  ›  专栏  ›  技术社区  ›  Schneems

使用Shoulda Ruby on Rails获取HTTP响应

  •  2
  • Schneems  · 技术社区  · 15 年前

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

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

    当我运行这个时,我得到一个错误,说@response是一个nill对象。

    2 回复  |  直到 15 年前
        1
  •  1
  •   dombesz    15 年前

    如果要访问响应,应首先将其包装为“应该”,如下所示:

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

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

        2
  •  0
  •   Greg Fairbrother    15 年前

    我认为shoulda语法是:

    你应该回答:成功吗

    而不是:

    你应该回答:成功