问题
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.
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.
What am I doing wrong?
回答1:
Wow, this was a tricky one.
I am using devise and wanted to test its views. In my view tests, I said:
describe "devise/registrations/new" do
# code here
end
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.
来源:https://stackoverflow.com/questions/14461981/rspec-view-test-does-not-render-changes