On Wednesday, June 18, 2014 7:24:35 AM UTC-7, Sebastian Trueman wrote:
>
> Hey,
>
> I'm a ruby beginner and I have a problem to keep my tests clean.
>
> My Code:
>
> require 'spec_helper'
>>
>>
>>> describe "Static pages" do
>>
>>
>>> def checkContentOfPage(content, page)
>>
>> it "should have the content '"+content+"'" do
>>
>> visit '/static_pages/'+page
>>
>> expect(page).to have_content(title)
>>
>> end
>>
>> end
>>
>>
>>> describe "Home page" do
>>
>>
>>>
>>
>> checkContentOfPage('Sample App', 'home')
>>
>>
>>> it "should have the title 'Home'" do
>>
>> visit '/static_pages/home'
>>
>> expect(page).to have_title("Ruby on Rails Tutorial Sample App |
>>> Home")
>>
>> end
>>
>> end
>>
>>
>>> describe "Help page" do
>>
>>
>>> it "should have the content 'Help'" do
>>
>> visit '/static_pages/help'
>>
>> expect(page).to have_content('Help')
>>
>> end
>>
>>
>>> it "should have the title 'Help'" do
>>
>> visit '/static_pages/help'
>>
>> expect(page).to have_title("Ruby on Rails Tutorial Sample App |
>>> Help")
>>
>> end
>>
>> end
>>
>>
>>> describe "About page" do
>>
>>
>>> it "should have the content 'About Us'" do
>>
>> visit '/static_pages/about'
>>
>> expect(page).to have_content('About Us')
>>
>> end
>>
>>
>>> it "should have the title 'About Us'" do
>>
>> visit '/static_pages/about'
>>
>> expect(page).to have_title("Ruby on Rails Tutorial Sample App |
>>> About Us")
>>
>> end
>>
>> end
>>
>> end
>>
>>
> I tried to create a method checkContentOfPage to reduce the have_content
> and have_title redundancy. But it doesnt work...
>
> Can you tell me how to keep my tests clean? It seems like java like
> refactoring is not possible.
>
> regards
>
Change `def checkContentOfPage` to `def self.checkContentOfPage` and it
should work. In a `describe` block, `self` is a class, and in an `it`
block, `self` is an instance of that class. When you define `def
checkContentOfPage` in a `describe` block, you are defining an instance
method that is available to be called from within an example (an `it`
block) but not from within a `describe` block. It needs to be a class
method (defined using `def self.`) for that to work.
You may also be interested in RSpec's shared examples feature:
https://relishapp.com/rspec/rspec-core/v/3-0/docs/example-groups/shared-examples
HTH,
Myron
--
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 [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/rspec/0981987c-e14b-4b1d-97c5-05f733fe6223%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.