IT:AD:SpecFlow:HowTo:Convert the Feature into runable UnitTests
Process
While you were typing the Feature file, it was automatically generating a CodeBehind File.
Right-Click a Step in a Scenario, and let it Generate the Script file.
It will be another file.
If you try to run the file, it will fail, saying that some of the steps are not complete… have to remove the 'Pending' and replace it with valid code.
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using TechTalk.SpecFlow;
namespace XAct.Spikes.SpecFLowConsoleApp.MSTests
{
[Binding]
public class SearchForFamousPeopleSteps
{
//To setup pre-tests vars and services, use one of
//[BeforeTestRun], [AfterTestRun], [BeforeFeature], [AfterFeature],
//[BeforeScenario], [AfterScenario], [BeforeScenarioBlock],
//[AfterScenarioBlock],[BeforeStep], [AfterStep]
[BeforeScenario]
public void Init()
{
MOE.NSI.POC.Application.AppBootstrapper.Initialize();
}
[Given(@"A First name of ""(.*)""")]
public void GivenAFirstNameOf(string p0)
{
//replace: ScenarioContext.Current.Pending(); with:
ScenarioContext.Current.Add("FirstName", p0);
}
[Given(@"a last name of ""(.*)""")]
public void GivenALastNameOf(string p0)
{
ScenarioContext.Current.Add("LastName", p0);
}
[When(@"I press Search")]
public void WhenIPressSearch()
{
//Pretend to call some service...
//And get back some results, which we store in the context
//to pass to the next method:
ScenarioContext.Current["Results"] = new List<string>() { "FakeResult1", "FakeREsult2", "etc." };
}
[Then(@"I should get back a list of one or more hits\.")]
public void ThenIShouldGetBackAListOfOneOrMoreHits_()
{
IList<string> list = (IList<string>)ScenarioContext.Current["Results"];
Assert.IsTrue(list.Count > 0);
}
[Given(@"The following first and last names:")]
public void GivenTheFollowingFirstAndLastNames(Table table)
{
ScenarioContext.Current.Pending();
}
//[Then(@"I should get back a list of more than one hits\.")]
//public void ThenIShouldGetBackAListOfMoreThanOneHits_()
//{
// IList<string> list = (IList<string>)ScenarioContext.Current["Result"];
// Assert.IsTrue(list.Count > 0);
//}
//[Then(@"I should get back a list of with zero entries\.")]
//public void ThenIShouldGetBackAListOfWithZeroEntries_()
//{
// ScenarioContext.Current.Pending();
//}
}
}