即使使用 config.render_views,RSpec response.body 仍然为空

发布于 2024-11-05 01:35:20 字数 491 浏览 1 评论 0原文

在我的spec_helper.rb 文件中,我专门将其设置为config.render_views,但我返回的response.body 仍然是空的。这是我的基本规格

describe  "#index" do
    it "should list all rooms" do
      get 'index'
      stub(Person).all
    end

    it "responds with 200 response code" do
      response.should be_ok
    end

    it "renders the index template" do
      pp response.body
      response.should render_template("people/index")
    end

  end

还有其他什么可以缩短这种行为吗?当我通过浏览器浏览时就很好了。我使用的是 Rspec 2.5.0

In my spec_helper.rb file I have specifically set it to config.render_views but the response.body I get back is still empty. Here is my basic spec

describe  "#index" do
    it "should list all rooms" do
      get 'index'
      stub(Person).all
    end

    it "responds with 200 response code" do
      response.should be_ok
    end

    it "renders the index template" do
      pp response.body
      response.should render_template("people/index")
    end

  end

Is there anything else that could have shorted this behavior? It's fine when I go through the browser. I am on Rspec 2.5.0

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

终难愈 2024-11-12 01:35:20

您是否尝试过在控制器规范文件中包含 render_views ?这对我有用。

我注意到的另一件事是,您在测试用例中只访问索引页一次 - 准确地说是第一个。其余的会因为没有响应而返回空的html内容。

这就是我将如何实现它。但是,如果 *spec_helper.rb* 文件中已有 config.render_views 且可以正常工作,则可以在控制器规范中不使用 render_views。

describe MyController
    render_views

    before :each do
        get :index
    end

    describe  "#index" do
        it "should list all rooms" do
            stub(Person).all
        end

        it "responds with 200 response code" do
            response.should be_ok
        end

        it "renders the index template" do
            pp response.body
            response.should render_template("people/index")
        end
    end
end

编辑:
这里的微妙变化是在 before blobk 中,我为每个 it 块调用 get :index

Have you tried having render_views in your controller spec file? That works for me.

Another thing I noticed is that you only access the index page once in your test cases - the first one to be precise. The rest will return empty html content because there is no response.

This is how I will implement it. But if you already have config.render_views in the *spec_helper.rb* file and that works, you can do without the render_views in the controller spec.

describe MyController
    render_views

    before :each do
        get :index
    end

    describe  "#index" do
        it "should list all rooms" do
            stub(Person).all
        end

        it "responds with 200 response code" do
            response.should be_ok
        end

        it "renders the index template" do
            pp response.body
            response.should render_template("people/index")
        end
    end
end

EDIT:
The subtle change here is the before blobk in which I call get :index for every it block.

苦妄 2024-11-12 01:35:20

我也遇到过同样的问题。

解决方案是指定请求的格式。

例如:
<代码>
获取:some_action,some_param:12121,格式:'json'

I've had the same issue.

The solution was to specify the format of the request.

For example:

get :some_action, some_param: 12121, format: 'json'

请恋爱 2024-11-12 01:35:20

这已从 RSpec 1 更改为 RSpec 2。视图规范现在使用 rendered 而不是 response

rendered.should =~ /some text/

更多github 上的发行说明中的信息。

This was changed from RSpec 1 to RSpec 2. View specs now use rendered instead of response:

rendered.should =~ /some text/

More info in the release notes on github.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文