Need help to write Nbehave example in StoryQ

Jul 14, 2009 at 11:56 PM
Edited Jul 14, 2009 at 11:58 PM
Here is example I wrote in NBehave:
How do I write this in StoryQ
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Specification = Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttribute;
using Context = Microsoft.VisualStudio.TestTools.UnitTesting.TestClassAttribute;
using NBehave.Narrator.Framework;
using NBehave.Spec.MSTest;


namespace NBehaveAccountExample
{
    [Context]
    public class Mstestexample: SpecBase
    {
        private Story _story;

        [Story]
        public override void MainSetup()
        {
            base.MainSetup();
            _story = new Story("Transfer to cash account");
            _story.AsA("savings account holder")
             .IWant("to transfer money from my savings account")
             .SoThat("I can get cash easily from an ATM");
        }
        [Specification]
        public void Savings_account_is_in_credit()
        {
            Account savings = null;
            Account cash = null;
            _story.WithScenario("Savings account is in credit")
                .Given("my savings account balance is $balance", 100, accountBalance => { savings = new Account(accountBalance); })
                    .And("my cash account balance is $balance", 10, accountBalance => { cash = new Account(accountBalance); })
                .When("I transfer $amount to cash account", 20, transferAmount => savings.TransferTo(cash, transferAmount))
                .Then("my savings account balance should be $balance", 80, expectedBalance => savings.Balance.ShouldEqual(expectedBalance))
                     .And("my cash account balance should be $balance", 30, expectedBalance => cash.Balance.ShouldEqual(expectedBalance));

        }

    }
    public class Account
    {
        public Account(int accountBalance)
        {
            Balance = accountBalance;
        }

        public int Balance { get; set; }

        public void TransferTo(Account account, int amount)
        {
            if (Balance > 0 && Balance >= amount)
            {
                account.Balance = account.Balance + amount;
                Balance = Balance - amount;
            }
        }
    }

}

 

 

Jul 21, 2009 at 11:15 PM

I figure it out, but please let me know if there is better way to do this

 [TestMethod]
        public void Savings_account_is_in_credit()
        {
            Account savings = null;
            Account cash = null;
            Story story = new Story("Transfer to cash account");
            story.AsA("savings account holder")
                .IWant("to transfer money from my savings account")
                .SoThat("I can get cash easily from an ATM")
                  .WithScenario("Savings account is in credit")
                   .Given(Narrative.Exec("my saving account balance is 100", () => { savings = new Account(100); }))
                      .And(Narrative.Exec("my cash account balance is 20", () => { cash = new Account(20); }))
                   .When(Narrative.Exec("I transfer 20 to cash account", () => { savings.TransferTo(cash, 20); }))
                  .Then(Narrative.Exec("my savings account balance should be 80", () => Assert.AreEqual(80, savings.Balance)))
                    .And(Narrative.Exec("my cash account balance should be 40", () => Assert.AreEqual(40, cash.Balance)));
            story.AssertAndReportToFile(MethodBase.GetCurrentMethod());



        }
Coordinator
Nov 17, 2009 at 3:18 PM

Hi Jagdeep. Sorry for not replying for so long. 

Yes: there's a better way. However that's going to be superceeded very soon when we release a new version of StoryQ. Is it ok if I use your code as an actual example when I document the new release?