require 'spec_helper'describe "Static pages" dodef checkContentOfPage(content, page)it "should have the content '"+content+"'" dovisit '/static_pages/'+pageexpect(page).to have_content(title)endenddescribe "Home page" docheckContentOfPage('Sample App', 'home')it "should have the title 'Home'" dovisit '/static_pages/home'expect(page).to have_title("Ruby on Rails Tutorial Sample App | Home")endenddescribe "Help page" doit "should have the content 'Help'" dovisit '/static_pages/help'expect(page).to have_content('Help')endit "should have the title 'Help'" dovisit '/static_pages/help'expect(page).to have_title("Ruby on Rails Tutorial Sample App | Help")endenddescribe "About page" doit "should have the content 'About Us'" dovisit '/static_pages/about'expect(page).to have_content('About Us')endit "should have the title 'About Us'" dovisit '/static_pages/about'expect(page).to have_title("Ruby on Rails Tutorial Sample App | About Us")endendend
You’re overwriting the capybara page method with your custom method.
In your spec:
describe "Home page" do
it "should have the title 'Home'" do
visit '/static_pages/home'
# The reference to `page` below is defined by capybara:
# https://github.com/jnicklas/capybara/blob/f83edc2a/lib/capybara/dsl.rb#L45
expect(page).to have_title("Ruby on Rails Tutorial Sample App | Home")
end
end
Your custom method creates a parameter called page. Ruby scoping rules apply thus you overwrite the definition of what page refers to:
def self.checkContentOfPage(content, page) # <--- you re-write `page` here
it "should have the content '"+content+"'" do # ^
visit '/static_pages/'+page # <---------| |
expect(page).to have_content(title) # <-| |
# These are not the capybara `page`, | |
# they are the parameter `page` |--------|
end
end
I would suggest renaming the parameter to something else. Also, as Myron pointed out, this looks like a shared_example, so you can use that feature as well if you want.
Myron and the rest of the team can correct me if I am wrong, but there is no official “coding standard” or “best practice” when it comes to RSpec. I know that’s not the answer you’re looking for, but RSpec is ever changing and evolving. Additionally, many of it’s users adjust their practices (I know I do) as we try new things out and decide if and when they work. At the end of the day the only things that should matter are those standards and practices that help you and your read consistently write readable / understandable specs.
--To view this discussion on the web visit https://groups.google.com/d/msgid/rspec/8cfaa405-5ecc-4efb-8992-e73464bfb7a4%40googlegroups.com.
You received this message because you are subscribed to the Google Groups "rspec" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rspec+un...@googlegroups.com.
To post to this group, send email to rs...@googlegroups.com.