Rspec 视图测试不呈现更改 [英] Rspec view test does not render changes

查看:43
本文介绍了Rspec 视图测试不呈现更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 rspec 测试 Rails 3.2 应用程序中的视图.

I am testing a view in my rails 3.2 application with rspec.

我已经为我的视图编写了一些测试以包含一些额外的输入字段,但它们正确地失败了.但是,添加所需的输入字段后,测试仍然以相同的方式失败.他们在终端中输出表单,就好像我没有更改视图中的任何内容一样.

I have wrote tests for my view to include some additional input fields, and they correctly failed. However, after adding the desired input fields, the tests still fail the same way. They output the form in the terminal, and it is as if I hadn't changed anything in the views.

在浏览器中检查视图时,字段实际上就在那里,因此测试应该通过.

When inspecting the view in the browser, the fields are in fact there, so the tests should pass.

rspec 没有加载最新的视图吗?

Has rspec not loaded the latest views?

这是一些代码(我将其缩减为两个字段):

Here is some code (I have reduced it to two fields):

it "renders the form to sign up" do
    rendered.should have_selector("form", action: "/users", method: "post") do |form|
        form.should have_selector("input#user_email", name: "user[email]", type: "email")
        form.should have_selector("input#user_city", name: "user[city]", type: "text")
    end
end

电子邮件输入是我以前使用的旧输入,它确实识别了它.城市输入是新的,它甚至没有出现在终端的视图输出中.

The email input is an old input that I had before, and it does recognize it. The city input is new, and it doesn't even appear in the terminal's view output.

我做错了什么?

推荐答案

哇,这是一个棘手的问题.

Wow, this was a tricky one.

我正在使用 devise 并想测试它的视图.在我看来测试中,我说:

I am using devise and wanted to test its views. In my view tests, I said:

describe "devise/registrations/new" do
  # code here
end

然而,前几天我切换到范围视图,并相应地将视图文件夹名称从 devise 更改为 users.如果您有不同的用户模型,例如 useradmin 并且需要不同的视图,那么作用域视图是很好的.您可以在 config/initializers/devise.rb 中打开范围视图,方法是将现有范围行替换为:

However, the other day I switched to scoped views, and changed the view folder name from devise to users accordingly. Scoped views are good if you have different user models such as user and admin and need different views for them. You can turn on scoped views in config/initializers/devise.rb by replacing the existing scope line with:

config.scoped_views = true

因此,我还必须将规范描述更改为

Hence, I also had to change my spec description to

describe "users/registrations/new" do # change to "users" instead of "devise"
  # code here
end

因为我没有这样做,它仍然呈现设计的标准视图,当它在我的视图文件夹中没有找到任何视图时它会呈现 - 因此我终端中的源代码输出表明它可以呈现正确的视图,但缺少更新的代码.

Since I didn't do this, it still rendered devise's standard views, which it renders when it doesn't find any views in my views folder - hence the source code output in my terminal, that suggested it could render the right views, but lacked the updated code.

这篇关于Rspec 视图测试不呈现更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆