Bespoke Software Solutions in C#.Net, ASP.Net MVC, JQuery, MEF and more
Posts tagged bdd
Writing Facebook Apps with ASP.NET MVC, Silverlight, LINQ2Facebook and BDD
Nov 10th
This weekend I looked into creating facebook applications from a .NET perspective. It turns out a quick search presents loads of support starting with the Facebook Developer Toolkit (Update: Just heard version 3 was released yesterday which gives silverlight and mvc support, wow what a coincidence?) and LINQ2Facebook (Fql).
I decided to do some research and ended up created a Facebook application framework using ASP.NET MVC, Silverlight, LINQ2Facebook. I also used StoryQ to POC their BDD framework and give the community a chance to see its power.
I have created a codeplex project with the source code here:
Facebook MVC BDD Silverlight Framework
Source code sample:
public ActionResult Index()
{
if (_service.TryAuthenticating())
{
user user = _service.GetCurrentUser();
var silverlightViewModel = new SilverlightViewModel
{
ApplicatonName = "TestApp",
Width = "400",
Height = "400"
};
silverlightViewModel.Params.Add("Name", user.name);
silverlightViewModel.Params.Add("Picture", user.pic_big);
return View(silverlightViewModel);
}
throw new AuthenticationException("Not allowed!");
}
[Test]
public void IndexActionShouldReturnViewModelWithCurrentLoggedInUser()
{
var story = new Story("Index action should return a new silverlight view model");
story.AsA("facebook application home page")
.IWant("to receive a new silverlight view model")
.SoThat("I can send it to the silverlight applicatioin")
.WithScenario("Get silverlight view model")
.Given(() => Steps.TheUserIsLoggedInWithUsername("Joe Blogs"))
.When(() => Steps.TheUserEntersTheHomePage())
.Then(() => Steps.EnsureTheCurrentLoggedInUserDetailsIsReceivedInTheViewModel());
story.Assert();
}
producing output:
Story: Index action should return a new silverlight view model
As a facebook application home page
I want to receive a new silverlight view model
So that I can send it to the silverlight applicatioin
Scenario 1: Get silverlight view model
Given The user is logged in with username (Joe Blogs) Passed
When The user enters the home page Passed
Then Ensure the current logged in user details is received in the view model Passed
Behaviour Driven Development in NUnit with StoryQ
Sep 22nd
A few months ago I started with BDD (Behaviour Driven Development). The first thing I noticed with most frameworks (MSpec, NBehave) is that you either need a new test runner tool (Galio) or need quite a bit of tweaking to implement the respective framework with Test-Driven.NET or Resharper’s test runner (Galio has a Resharper plugin).
This isn’t a problem in most cases, but what if you need to run these tests on Team-City or Cruise-Control? Now the complexity of the tweaking starts to become quite a different ball game!
Luckily there is an open-source tool called StoryQ which runs on NUnit and doesnt require any tweaking. The developer can run this with his usual test-runner tool and best of all, nothing has to change on the Buildserver.
Besides requiring no tweaking, StoryQ is a pretty competitive BDD tool altogether and provides loads of nifty features.
The idea is that “Tests” should be viewed as “Specs”, because this removes much of the confusion of TDD especially for beginners.
Your typical Unit test (or Spec) would look like this:
using NUnit.Framework;
using StoryQ.Framework;
namespace StoryQ.Tests.Documentation
{
[TestFixture]
public class SimpleDocumentationSpec
{
[Test]
public void WritingSpecificationsToBegin()
{
Story story = new Story("writing executable specifications");
story.AsA("business person")
.IWant("to write executable specifications")
.SoThat("a developer can implement them")
.WithScenario("Writing specifications")
.Given("That i have written everything in text")
.When("the test is run")
.Then("the test result should be pending")
.WithScenario("Writing specifications again")
.Given("That i have written everything in text")
.And("still have no asserts")
.And("and have another condition")
.When("the test is run")
.And("I am happy")
.Then("the test result should be pending")
.And("here's just some more and's");
story.Assert();
}
}
}
This would present an NUnit output result of:
SimpleDocumentationSpec.WritingSpecificationsToBegin : IgnoredPending
Story: writing executable specifications
As a business person
I want to write executable specifications
So that a developer can implement them
Scenario 1: Writing specifications
Given That i have written everything in text *Pending
When the test is run *Pending
Then the test result should be pending *Pending
Scenario 2: Writing specifications again
Given That i have written everything in text *Pending
And still have no asserts *Pending
And and have another condition *Pending
When the test is run *Pending
And I am happy *Pending
Then the test result should be pending *Pending
And here's just some more and's *Pending
You can then start adding functionality with the given scenarios like so:
using NUnit.Framework;
using StoryQ.Framework;
namespace StoryQ.Tests.Documentation
{
[TestFixture]
public class UsingNarrativesSpec
{
[Test]
public void WritingExecutablePassingSpecificationsWithNarratives()
{
Story story = new Story("writing executable specifications");
story.AsA("business person")
.IWant("to write executable specifications")
.SoThat("a developer can implement them")
.WithScenario("Writing specifications again")
.Given(Narrative.Text("I have a narrative"))
.When(() => TheTestIs_("run"))
.Then(Narrative.Exec("this will now pass", passMe));
story.Assert();
}
static void passMe()
{
Assert.IsTrue(true);
}
static void TheTestIs_(string s)
{
Assert.AreEqual(s, "run");
}
}
}
Which will give you the output of:
UsingActionsSpec.WritingExecutableSpecificationsAsActions : PassedStory: writing executable specifications
As a business person
I want to write executable specifications
So that a developer can implement them
Scenario 1: Writing specifications
Given That i have written everything in text *Pending
When the test is run *Pending
Then the test result should be pending *Pending
Scenario 2: Writing specifications again
Given That everything isnt Passed
When The test is run Passed
Then the test result should be pending *Pending
There are many more options and different ways to implement the scenarios on the StoryQ home page.
Another great feature about StoryQ is to convert your english written stories into C# tests. Yes, you read that right! It will even write the code for you! By using convention that can be tought to your “Product Owners”, you can literally copy and paste their requirements to a tool which will present the generated c# test.