Skip to content
MorganPersson edited this page Nov 26, 2012 · 1 revision

Do you work on a project where you have list's of stuff? Well then table steps might be useful for you.

Lets look at an example:

Feature: Table support in nbehave
Scenario: a table
  Given a list of books:
    | Title | Isbn |
    | aaa   | 1234 |
    | bbb   | 2233 |
    | ccc   | 8899 |
    | ccaa  | 9999 |
  When I search for books that have 'a' in their title
  Then I should find:
    | Title | Isbn |
    | aaa   | 1234 |
    | ccaa  | 9999 |

And here is the code to implement the feature

using System.Collections.Generic;
using System.Linq;
using NBehave.Narrator.Framework;
using NUnit.Framework;

namespace NBehavePlay
{
    [ActionSteps]
    public class StepsImpl
    {
        [Given("a list of books:")]
        public void SomeBooks(List<Book> books)
        {
            ScenarioContext.Current.Add("books", books);
        }

        [When("I search for books that have '$titleSearch' in their title")]
        public void When_I_search(string titleSearch)
        {
            var books = ScenarioContext.Current.Get<List<Book>>("books");
            var found = books.Where(_ => _.Title.Contains(titleSearch)).ToList();
            ScenarioContext.Current["found"] = found;
        }

        [Then("I should find:")]
        public void Then_I_should_find_book_with_title_title(List<Book> expectedToFind)
        {
            var actual = ScenarioContext.Current.Get<List<Book>>("found");
            CollectionAssert.AreEquivalent(expectedToFind, actual);
        }
    }
}
Clone this wiki locally