mickael
mickael

Reputation: 2095

NoMethodError: undefined method `expect' when trying to use it within a SitePrism class

I've got a cucumber,ruby,siteprism project where we're using the 'rspec' gem to check expectations. This is included in our env.rb and used successfully in the step definitions.

I was now trying to make some assertions in a SitePrism class but I am getting an error. Do you know how I could use those expect() methods? I tried with require 'rspec' plus include Rspec in the .rb file which is defining the SitePrism class, but I got the same error still:

expect(local_value).to eq(@previous_value)
=> Error: NoMethodError: undefined method `expect' for #<xxx_Object @loaded=false>

Thank you!

Upvotes: 6

Views: 8168

Answers (2)

Luke Hill
Luke Hill

Reputation: 458

Old question but providing answer

It's worth pointing out as well that when following the README instructions. Cucumber will have the RSpec testing functions loaded into the cucumber world.

Dependent on who you speak to and where; it is preferable (Arguably), to perform all RSpec based testing of your features inside the Cucumber World (i.e. in step_definitions).

Furthermore, doing this avoids you needing to include these items anywhere, and you'll get clean steps such as expect(my_page.header_message.text).to eq('This')

You could also use any other one, include the auto-created capybara methods which would use implicit waiting or rspec auto-included ones created from methods on your class

Upvotes: 1

Thomas Walpole
Thomas Walpole

Reputation: 49910

As you've discovered (from your comment) you can include RSpec::Matchers in your page object class to get expect along with RSpecs default matchers. One of those matchers is named all which then shadows the Capybara::DSL all method that previously included into the object, and produces the error you're seeing. The way to solve that is to call the Capybara version of all on the current_session object (page) or the alias 'find_all'. So all(...).map(...) becomes

page.all(...).map(...) # Same as Capybara.current_session.all(...)...

or

find_all(...).map(...)  # or page.find_all ...

Upvotes: 5

Related Questions