Undefined method 'should' when using Capybara with Cucumber
Asked Answered
F

3

5

I am trying to use both Capybara and Cucumber in my Rails application. So, that's what i did:

  • Installed gems needed and added them to the Gemfile
  • Ran the rails generate capybara:install --cucumber
  • Created a feature and its steps definitions for cucumber

Here's my feature (yeah, i am creating blog application):

Feature: Browse posts
    So that I can browse through the posts
    As a visitor
    I want to see all and/or particular posts

    Scenario: Viewing all the posts
        Given Posts exist
        When I navigate to the website home
        Then I should see all the posts

    Scenario: Viewing particular post
        Given Post #1 exists
        When I navigate to /posts/view/1
        Then I should see the post with id=1

And here's its step definitions:

Given /^Posts exist$/ do
    assert (not Post.all.empty?), "No posts found at all"
end

Given /^Post #(\d+) exists$) do |id|
    assert Post.find_by_id(id).valid?, "Post ##{ id } was not found at all"
end

When /^I navigate to (.+)$/ do |url|
    if url =~ /^the website home$/ then
        visit '/'
    else
        visit url
    end
end

Then /^I should see all(.+)posts$/ do |delimiter|
    posts = Post.all

    posts.each do |post|
        page.should have_content post.title
    end
end

Then /^I should see the post with id([^\d]{1,})(\d+)$/ do |delimiter, id|
    post = Post.find_by_id id

    page.should have_content post.title
end

And when running rake cucumber i get this message:

Then I should see all the posts     # features/step_definitions/browsing_posts.rb:13
  undefined method `should' for #<Capybara::Session> (NoMethodError)
  ./features/step_definitions/browsing_posts.rb:17:in `block (2 levels) in <top (required)>'
  ./features/step_definitions/browsing_posts.rb:16:in `each'
  ./features/step_definitions/browsing_posts.rb:16:in `/^I should see all(.+)posts$/'
  features/browsing_posts.feature:9:in `Then I should see all the posts'

What am I doing wrong? And yeah, are there any mistakes in my feature?

Feast answered 29/8, 2012 at 11:28 Comment(5)
This won't solve your problem but improve the quality of your stories: Given nothing is not correct. Given Posts exist is more likely what you want.Sophistication
are you using the default test::unit or with rspec? some of your syntax are more in the style of rspecHalvaard
@Halvaard actually, i am not sure =) i run tests with rake cucumber and i do not see any rspec or test::unit code provided by me. i suppose cucumber uses its own classesFeast
In that case you are defaulting to test-unit and should use the expectation syntax that comes with itHalvaard
@Halvaard what's that expectation syntax??? I did not got the idea of your thoughts - are you talking about assert's???Feast
H
17

Replace page.should have_content post.title with assert page.has_content?(post.title). If that works, apply that similarly to the other have_content statements

Edit: Those statements involving should are based on rspec expectations, and should be used only if you are already using rspec or some other testing framework that responds_to should. Coming from the test::unit angle, this is probably just another kind of assert.

Halvaard answered 29/8, 2012 at 12:19 Comment(2)
Where do you find list of "asserts" that working for Minitest?Servility
@JohanDonadoB. guides.rubyonrails.org/testing.html#available-assertions would be a good starting pointHalvaard
U
4

can you try

page.has_content?('foo')

instead of

page.should have_content post.title

if it works, then you can use this with assert.

Ustkamenogorsk answered 29/8, 2012 at 12:23 Comment(0)
R
2

after facing the same issue I tried

expect(page).to have_content('foo')

which worked fine for me

Rife answered 1/9, 2013 at 6:12 Comment(1)
This indicates that you were using a newer version of RSpec.Mckeever

© 2022 - 2024 — McMap. All rights reserved.