How to expect an unsuccessful step and miss a failure in Cucumber?

We want to check our step definitions for cucumber. One thing that we would like to test is that the tests we expect to fail actually fail. To do this, we would like to write scripts that we know will fail and add them to our test suite, but tag them or otherwise label them so that they “pass” if and only if they fail. How to do it?

+5
source share
3 answers

You must test a negative state. An unsuccessful step is simply the reverse step. So do something like:

Then /i should not be true/ do
  some_value.should_not be_true
end

. .. ,

lambda do
  something_that_horks
end.should raise_error(Specific::Error)

, , .

0

, - , . , . , , , . .

/step_definitions/before_step.rb

Before("~@fails") do
  def assert_cucumber(assersion, msg = "an error was thrown")
    assert(assersion == true, msg)
  end
end

Before("@fails") do
  def assert_cucumber(assersion, msg = "an error was thrown")
    assert(assersion == false, msg)
  end
end

/step_definitions/user_step.rb

Given /^a user with$/ do |params|
  params = params.rows_hash
  unless User.find_by({username: params[:username]})
    assert_cucumber(User.new(params).save, "could not create user")
  end
end

/user.feature

Scenario: check if userers exsist
  Given a user with
    | username | johnsmith             |
    | email    | johnsmith@example.com |
    | password | password              |
  Then a user with username "johnsmith"

@fails
Scenario: create user with missing data
  Given a user with
    | username | johndoe |
  Then a user with username "johndoe"
0

-w Cucumber.

It will output the normal format, but in the end it will give a brief description of whether all the test cases failed, and if they pass, it will indicate which ones.

-1
source

All Articles