Alastair
Alastair

Reputation: 1800

How do I test conditional behaviour with Behat/Mink?

I have a condition I'd like to test in my Behat scenario but I can't think how it should be described.

I am testing a webpage displaying articles - if there is one sponsored article then there should be two featured articles, if there is no sponsored article then there should be three feature articles.

How do I approach this in Gherkin? If I do, say, Given there is one sponsored article then as soon as there is no sponsored article, that test will fail.

Is this beyond the scope of the tool? If so how would I go about testing this scenario?

Upvotes: 0

Views: 1980

Answers (1)

Jakub Zalas
Jakub Zalas

Reputation: 36241

Scenarios should be deterministic. This means that if you rely on a data from a database, it needs to be deterministic as well.

You can achieve it in multiple ways.

One way is to pre-populate your database with known data, either before the tests run or before each scenario. Some of the data will be build during the scenario is run. This might be expensive, but most reliable.

Another way is to maintain database with fixtures for testing. The test suite will be quicker this way, but you have to be careful with changes you make during the test execution.

Which way is better to choose depends on which compromises you're willing to make.

You'll probably want to prepare at least two scenario to cover the edge cases:

Scenario: One sponsored article

Scenario: No sponsored articles 

Unless you can visit two distinct pages to reproduce both scenarios, you'll have to pre-populate (or update) the database before each one of them.

In your step:

Given there is one sponsored article

you should either create a sponsored article (if you're populating the database for each scenario), or verify it exists (if you rely on a pre-populated database).

Upvotes: 4

Related Questions