Reputation: 2233
I'm writing some RSpec tests with Capybara in a Rails app and I can use some advice. I'd like to load the app's home page and then test that certain elements are present on the page. To do that, I'm writing something like
context "Logo is present" do
it "is configured properly" do
visit root_path
expect(page.find("a.logo")[:alt]).to have_content("blah")
expect(page.find("a.title")[:alt]).to have_content("title")
... 4 more expect lines here ...
end
end
I don't particularly like having a bunch of these expect statements in the same it
block. I'd rather have separate it blocks and do something like this:
context "Logo is present" do
before(:all) {visit root_path}
it "has a valid alt" do
expect(page.find("a.logo")[:alt]).to have_content("blah")
end
it "has a valid title" do
expect(page.find("a.title")[:alt]).to have_content("title")
end
... 4 more it lines here ...
end
The problem is the call to before(:all) {visit root_path}
. I had hoped that would run one time before all the it
blocks. Unfortunately, it looks like Capybara tears down the page after the first it
block is run, so all it
blocks fail except the first one.
Is there a way I can visit a page one time and have multiple it
blocks that use that page? I'm trying to avoid paying the price of visiting the page multiple times when having separate it
blocks.
Upvotes: 2
Views: 404
Reputation: 49870
Nope - the whole point of rspec tests is that they are isolated from each other. Also, the type of tests you're showing really seem more like they should be view specs (the capybara matchers should be available in view specs) than feature specs.
Upvotes: 1