Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Introducing BDDfy: the simplest BDD framework for .Net

, 23 Sep 2013
This article is an introduction to installing and using bddify - a simple to use and extend BDD framework for .NET developers.
Bddify.Samples.Net4.zip
Bddify.Samples
Bddify.Samples.Atm
Bddify.Samples.suo
Bddify.Samples.TicTacToe
packages
Bddify.0.8
Bddify.0.8.nupkg
content
lib
NET35
Bddify.dll
NET40
Bddify.dll
SL4
Bddify.dll
Bddify.Samples.Atm.0.8
Bddify.Samples.Atm.0.8.nupkg
content
Bddify.Samples.Atm
Bddify.Samples.TicTacToe.0.8
Bddify.Samples.TicTacToe.0.8.nupkg
content
Bddify.Samples.TicTacToe
NUnit.2.5.10.11092
lib
nunit.framework.dll
nunit.mocks.dll
pnunit.framework.dll
Logo.ico
NUnit.2.5.10.11092.nupkg
tools
agent.conf
agent.log.conf
launcher.log.conf
lib
Failure.png
fit.dll
Ignored.png
Inconclusive.png
log4net.dll
nunit-console-runner.dll
nunit-gui-runner.dll
nunit.core.dll
nunit.core.interfaces.dll
nunit.fixtures.dll
nunit.uiexception.dll
nunit.uikit.dll
nunit.util.dll
Skipped.png
Success.png
nunit-agent-x86.exe
nunit-agent.exe
nunit-console-x86.exe
nunit-console.exe
nunit-x86.exe
nunit.exe
nunit.framework.dll
NUnitTests.nunit
pnunit-agent.exe
pnunit-launcher.exe
pnunit.framework.dll
pnunit.tests.dll
runFile.exe
test.conf
RazorEngine.2.1
lib
.NetFramework 4.0
RazorEngine.dll
System.Web.Razor.dll
RazorEngine.2.1.nupkg
Properties
Bddify.Samples.V0.9.zip
Bddify.Samples.suo
Bddify.0.9
Bddify.0.9.nupkg
content
lib
NET35
Bddify.dll
NET40
Bddify.dll
SL4
Bddify.dll
Bddify.Samples.Atm.0.9
Bddify.Samples.Atm.0.9.nupkg
content
Bddify.Samples.Atm
Bddify.Samples.TicTacToe.0.9
Bddify.Samples.TicTacToe.0.9.nupkg
content
Bddify.Samples.TicTacToe
nunit.framework.dll
nunit.mocks.dll
pnunit.framework.dll
Logo.ico
NUnit.2.5.10.11092.nupkg
agent.conf
agent.log.conf
launcher.log.conf
Failure.png
fit.dll
Ignored.png
Inconclusive.png
log4net.dll
nunit-console-runner.dll
nunit-gui-runner.dll
nunit.core.dll
nunit.core.interfaces.dll
nunit.fixtures.dll
nunit.uiexception.dll
nunit.uikit.dll
nunit.util.dll
Skipped.png
Success.png
nunit-agent-x86.exe
nunit-agent.exe
nunit-console-x86.exe
nunit-console.exe
nunit-x86.exe
nunit.exe
nunit.framework.dll
NUnitTests.nunit
pnunit-agent.exe
pnunit-launcher.exe
pnunit.framework.dll
pnunit.tests.dll
runFile.exe
test.conf
RazorEngine.dll
System.Web.Razor.dll
RazorEngine.2.1.nupkg
using Bddify.Core;
using NUnit.Framework;


namespace Bddify.Samples.TicTacToe
{
    [Story(
        AsA = "As a player",
        IWant = "I want to have a tic tac toe game",
        SoThat = "So that I can waste some time!")]
    [TestFixture]
    public class TicTacToe : NewGame
    {
        class Cell
        {
            public Cell(int row, int col)
            {
                Row = row;
                Col = col;
            }

            public int Row { get; set; }
            public int Col { get; set; }

            public override string ToString()
            {
                return string.Format("({0}, {1})", Row, Col);
            }
        }

        void GivenTheFollowingBoard(string[] firstRow, string[] secondRow, string[] thirdrow)
        {
            Game = new Game(firstRow, secondRow, thirdrow);
        }

        void ThenTheBoardStateShouldBe(string[] firstRow, string[] secondRow, string[] thirdrow)
        {
            Game.VerifyBoardState(firstRow, secondRow, thirdrow);
        }

        void ThenTheWinnerShouldBe(string expectedWinner)
        {
            Assert.AreEqual(expectedWinner, Game.Winner);
        }

        void ThenItShouldBeACatsGame()
        {
            Assert.IsNull(Game.Winner);
        }

        void WhenTheGameIsPlayedAt(params Cell[] cells)
        {
            foreach (var cell in cells)
            {
                Game.PlayAt(cell.Row, cell.Col);
            }
        }

        [Test]
        public void CatsGame()
        {
            this.Given(s => s.GivenTheFollowingBoard(new[] { X, O, X }, new[] { O, O, X }, new[] { X, X, O }), BoardStateTemplate)
                .Then(s => s.ThenItShouldBeACatsGame())
                .Bddify("Cat's game");
        }

        [Test]
        public void WhenXAndOPlayTheirFirstMoves()
        {
            this.Given(s => s.GivenANewGame())
                .When(s => s.WhenTheGameIsPlayedAt(new Cell(0, 0), new Cell(2, 2)), "When X and O play on {0}")
                .Then(s => s.ThenTheBoardStateShouldBe(new[] { X, N, N }, new[] { N, N, N }, new[] { N, N, O }))
                .Bddify();
        }

        [Test]
        public void OWins()
        {
            var cell = new Cell(2, 0);
            this.Given(s => s.GivenTheFollowingBoard(new[] { X, X, O }, new[] { X, O, N }, new[] { N, N, N }))
                .When(s => s.WhenTheGameIsPlayedAt(cell))
                .Then(s => s.ThenTheWinnerShouldBe(O))
                .Bddify();
        }

        [Test]
        public void XWins()
        {
            // This is implemented like this to show you the possibilities
            new XWins().Bddify();
        }

        [Test]
        [TestCase("Vertical win in the right", new[] { X, O, X }, new[] { O, O, X }, new[] { O, X, X }, X)]
        [TestCase("Vertical win in the middle", new[] { N, X, O }, new[] { O, X, O }, new[] { O, X, X }, X)]
        [TestCase("Diagonal win", new[] { X, O, O }, new[] { X, O, X }, new[] { O, X, N }, O)]
        [TestCase("Horizontal win in the bottom", new[] { X, X, N }, new[] { X, O, X }, new[] { O, O, O }, O)]
        [TestCase("Horizontal win in the middle", new[] { X, O, O }, new[] { X, X, X }, new[] { O, O, X }, X)]
        [TestCase("Vertical win in the left", new[] { X, O, O }, new[] { X, O, X }, new[] { X, X, O }, X)]
        [TestCase("Horizontal win", new[] { X, X, X }, new[] { X, O, O }, new[] { O, O, X }, X)]
        public void WinnerGame(string title, string[] firstRow, string[] secondRow, string[] thirdRow, string expectedWinner)
        {
            new WinnerGame(firstRow, secondRow, thirdRow, expectedWinner).Bddify<TicTacToe>(title);
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Mehdi Khalili
Software Developer (Senior) ThoughtWorks
United States United States
I work as a Senior Consultant for ThoughtWorks
Follow on   Twitter   Google+   LinkedIn

| Advertise | Privacy | Mobile
Web01 | 2.8.140721.1 | Last Updated 23 Sep 2013
Article Copyright 2011 by Mehdi Khalili
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid