0

i'm using following:

minitest 5.3.5
cucumber 1.3.15
capybara 2.3.0
capybara_minitest_spec 1.0.2
rails 4.1.1
cucumber-rails 1.4.1

and my cucumber/capybara code

Then(/^I should see "(.*?)"$/) do |string|
  page.has_content?(string)
end

with any string, independent is it on the page or not returns true. I've tried to use capybara_minitest_spec gem and my code becomes

Then(/^I should see "(.*?)"$/) do |string|
  page.must_have_content(string)
end

then i've got error

Then I should see "Test" # features/step_definitions/pages_steps.rb:9
  undefined method `assert_page_has_content' for nil:NilClass (NoMethodError)
  (eval):4:in `must_have_content'
  ./features/step_definitions/pages_steps.rb:10:in `/^I should see "(.*?)"$/'
  features/can_access_home.feature:7:in `Then I should see "Test"'

So strange. With save_and_open_page i see my page without any problems. Could you help me please to fix it?

PS: i'm using minitest but not rspec

Alexey Poimtsev
  • 2,845
  • 3
  • 35
  • 63

2 Answers2

0
Then(/^I should see "(.*?)"$/) do |string|
  page.has_content?(string)
end

This is not an assertion, this is a method that returns a boolean. Assertions throw exceptions, which is what makes test fail.

If you want to assert existence of your string, you have to use :

Then(/^I should see "(.*?)"$/) do |string|
  assert page.has_content?("Home#index")
end

page.must_have_content actually does the same thing, except your error says page is nil.

kik
  • 7,867
  • 2
  • 31
  • 32
  • I've got undefined method `expect' for # (NoMethodError) for your last example – Alexey Poimtsev Jun 25 '14 at 09:43
  • My bad, I don't know why I thought you were using rpsec. Well, first observation still stands, but there's a visit problem. Updating anwser. – kik Jun 25 '14 at 09:49
  • Ok, we don't know why `page` was not defined the first time you tried to use `#must_have_content`. I still leave this answer for further reference as it explains you're supposed to assert your expectation rather than just returning a boolean. – kik Jun 25 '14 at 10:18
0

I've fixed problem with following

Then(/^I should see "(.*?)"$/) do |string|
  assert page.has_content?(string)
end
Alexey Poimtsev
  • 2,845
  • 3
  • 35
  • 63
  • Yep, you have to assert. Except there's no reason page is defined correctly here, and is nil when you use `page.must_have_content(string)`. Can you double check this second form does not work ? – kik Jun 25 '14 at 09:54
  • undefined method `must_have_content' for # (NoMethodError) :((( – Alexey Poimtsev Jun 25 '14 at 10:11
  • Well, that changes from your initial error :) You probably have to include matchers somewhere in your cucumber setup. Anyway, using `assert` is just as fine. – kik Jun 25 '14 at 10:15