2

I'm using rspec expectations in a cucumber framework and they look fine when used at the steps definition level.

I've configured my env.rb file with:

require 'rspec/expectations'
World(RSpec::Matchers)

The problem I've noticed now is that if I try to use rspec inside a method of an object that's used inside one of the steps then I've got a failure.

E.g.
Steps_definition.rb
   service.use_rspec

class Service
   def use_rspec
       header = page.find("div#services h2").text
       header.should (be 'TV')
   end
 end

Error after execution:
 undefined method `be' for #<Service:0x2592570> (NoMethodError)

Any idea where the problem could be?

I've tried a similar assertion with Capybara.page.find(...).should have_content('...') inside that class and 'have_content' is not recognized either, so not really sure what's going on :S

Many thanks for any tip!

Dave Schweisguth
  • 36,475
  • 10
  • 98
  • 121
mickael
  • 2,033
  • 7
  • 25
  • 38

1 Answers1

-1

Your Service class isn't in the World so RSpec::Matchers isn't available there.

You have two possibilities:

  1. Include RSpec::Matchers manually to this class.
  2. Put this class (or module) to World. After that its methods will become directly available in step definitions.

Write:

class Helpers
  def method
    # Capybara and RSpec::Matchers are available here
  end
end
World{Helpers.new}

or

module Helpers
  def method
    # Capybara and RSpec::Matchers are available here
  end
 end
World(Helpers)
Andrei Botalov
  • 20,686
  • 11
  • 89
  • 123
  • Hi Andrey. Not sure where is my problem then. I had put created a module which I put to the World where I had created the service object, so the service methods were available in the steps, that was fine. The problem I have (even after trying the suggestions above) is that **#Capybara and Rspec::Matchers don't seem to be recognized inside the class.** If I try: `include RSpec::Matchers` inside the class then I get the following error `undefined method `new' for RSpec::Matchers::Pretty:Module Error creating formatter: pretty (NoMethodError)` – mickael Dec 15 '12 at 15:49
  • @mickael it works for me. Try to start from scratch. Capybara and RSpec::Matchers are available inside module or class if it's put to World – Andrei Botalov Dec 15 '12 at 16:39