Reputation: 889
I know in a normal feature spec using Capybara, I could do something like this:
require "rails_helper"
describe "users/show", type: :feature do
it 'renders a page' do
visit user_path
save_and_open_page
end
end
Is there an equivalent way to view the rendered page in a view spec, without tagging it as a feature? Ie something like this:
require "rails_helper"
describe "users/show", type: :view do
it 'renders a page' do
render
save_and_open_page
end
end
As is, I get a NameError: undefined local variable or method 'save_and_open_page'
raised when I try this.
(I know I could just view the string as rendered, and this method someone suggested in a similar thread on feature specs works - and I could write my own method to do that if need be. But it seems likely there'd be some existing functionality for it?)
Upvotes: 3
Views: 1880
Reputation: 976
Depending on your needs it might be enough to simply create a Capybara::Node::Simple
object from the rendered
string in your view spec by following the approach shown in the article validating views with capybara queries.
As taken from the article you can do:
it "does not render the account link when signed out" do
... # assign and render a view or template
doc = Capybara.string(rendered)
expect(doc).to have_no_css("nav a", text: "Account")
end
Note that this will allow you to use Capybara Finders methods (e.g. #find
) on the doc element:
doc.find('p')
For more guidance on this topic have of a look at the card RSpec: Leverage the power of Capybara Finders and Matchers for view specs
Upvotes: 2
Reputation: 49910
No, there is no existing functionality in view tests for opening the "page" in a browser. By default only Capybaras matchers are included into view specs, not its session methods. This is because view specs don't have a "browser", it's just your view templates being rendered and returned as a string (no CSS/JS loaded, no cookies, no real session, no interaction, etc). As you pointed out it's easy enough to save the rendered
string to a file and load into a browser if you actually need that functionality.
Upvotes: 2