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

2

6

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!

Gallinule answered 15/2, 2017 at 12:8 Comment(1)
I included include RSpec::Matchers in the class and that seems to pass the expect error message. However, I then got another related one, any suggestion for fixing this error, or including the expect in a different way? NoMethodError: undefined method `map' for #<RSpec::Matchers::BuiltIn::All:0x000000067a5ac0> Did you mean? tapGallinule
K
5

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 ...
Kauppi answered 15/2, 2017 at 17:1 Comment(13)
This is related to the other question you helped me with about SitePrism. #42237083 I was just invoking a siteprism method without calling Capybara myself. Do you know if that can be done in a different way to bypass this problem?Gallinule
@Gallinule all isn't a site_prism method, it's a Capybara method that site_prism includes into the page object class. If you're saying that your code isn't calling all but somewhere in site_prism is, then you will need to run with the master branch of site_prism. There was a fix merged end of last year that fixes it - github.com/natritmeyer/site_prism/pull/162Kauppi
I see, yes, it seems that site_prism it's calling it when working with sections then. One more basic doubt though. I've got the gem version 2.9 installed, and checking rubygems.org, it seems that's the latest. Do you have a link about how I could upgrade my version to master branch if it has not been officially released? (as I guess, I could overwrite some files locally, but the framework is used by other people in the team as well)?Gallinule
In your gemfile rather than version 2.9 you can specify , gem 'site_prism', git: 'https://github.com/natritmeyer/site_prism.git' to tell it use the master branch from the github repo (you can pass a :branch option too if you ever need a branch other than master)Kauppi
Hmm... it didn't seem to work: * Using site_prism 2.9 from github.com/natritmeyer/site_prism.git (at master@307d19e) * Then I was using the following expectation in a method within my SitePrism page: expect(postcode).to eq("E1") * And got the same error: NoMethodError: undefined method `map' for #<RSpec::Matchers::BuiltIn::All:0x0000000563a5d8> Am I doing something wrong that you can see from your end?Gallinule
What did you end up defining postcode as ? and what does the stacktrace show as the location of the all call?Kauppi
def postcode() user_container[2].address_module[1].house_number[3].text end And the error mentions: from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/site_prism-2.9/lib/site_prism/element_container.rb:38:in `block (2 levels) in sections'Gallinule
Sorry, forgot to mention. In the site_prism denition I used :elements (rather than :element). ie: sections :user_container, "#user_container" do sections :address_module, "#address" do elements :house_number, "#house_number" end endGallinule
Umm -- usually when using a gem from a github repo the directory name would be /site_prism-<SHA>/... not the version number, so maybe you're not actually using the latest version - Did you restart spring (or any other app preloaders you're using)?Kauppi
Ah, I just installed it on top of the previous one. I'll uninstall it completely tomorrow, reinstall the new one and retry. I'll let you know how it goes. Many thanks!!!Gallinule
Using bundle install doesn't seem to install the site_prism gem from git. It says: Using site_prism 2.9 from https://github.com/natritmeyer/site_prism.git (at master@307d19e) but if I check with 'gem list' it has not installed anything.Gallinule
Trying to install it with: gem install site_prism, git: 'http://github.com/natritmeyer/site_prism.git' but getting another error: ERROR: Could not find a valid gem 'site_prism,' (>= 0), here is why: Unable to download data from https://rubygems.org/ - SSL_connect returned=1 errno=0 state=SSLv3 read server certificate B: certificate verify failed (https://api.rubygems.org/specs.4.8.gz) I need to move to another project for a day or two, so I'll try to recheck those errors in the coming days to double check. Thank you.Gallinule
Hey @Thomas, you were correct. It seems something changed in Site Prism and that issue with the .map error got fixed when using the master branch. Strange that a new version has not been released after almost 1 year, even when there are fixes and 10 commits to master though. Marking this as clarified. Thanks for your help!Gallinule
D
1

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

Diplocardiac answered 22/2, 2019 at 14:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.