简体   繁体   English

使用Shoulda Ruby on Rails获得HTTP响应

[英]Get HTTP response Using Shoulda Ruby on Rails

I'm migrating over to shoulda from rspec and I can't seem to get access to the http response. 我正在从rspec迁移到Shoulda,但似乎无法访问http响应。 Can someone point out what I may be doing wrong? 有人可以指出我可能做错了什么吗?

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

When i run this i get an error saying that @response is a nill object. 当我运行它时,我得到一个错误,说@response是一个nill对象。

If you want to access the response you should first wrap into a "should" like this: 如果要访问响应,则应首先将其包装为“应该”,如下所示:

context "doing somethin" do
  setup do
    get :index
  end

  should "i access..." do
   assert response.status, 200
  end
end

It's like you try to use the response outside a test, each should represents a test case, and a context is like a before(:each) in rspec. 就像您尝试在测试之外使用响应一样,每个响应都应代表一个测试用例,并且上下文类似于rspec中的before(:each)。

I believe the shoulda syntax is: 我相信应该的语法是:

should_respond_with :success should_respond_with:成功

instead of: 代替:

should respond_with :success 应该response_with:success

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM