Context
I am relatively new to .NET and have decided to use BDD in a project. I'm using Specflow for this.
I have created a feature file using the Gherkin format and generated step definitions.
I'm using Selenium to insert the information from a table in my feature file into the web page and I'm using MSTest to test the results.
My Step Definitions
[Binding]
public class RegisterSteps
{
private IWebDriver ff = new FirefoxDriver();
private string username = "";
[Given(@"you are on the register page")]
public void GivenYouAreOnTheRegisterPage()
{
ff.Navigate().GoToUrl("http://localhost:55475/Register");
}
[Given(@"you enter the following information")]
public void GivenYouEnterTheFollowingInformation(TechTalk.SpecFlow.Table table)
{
username = table.Rows[6]["Value"];
for (var i = 0; i < table.RowCount; i++)
{
var field = table.Rows[i]["Field"];
var value = table.Rows[i]["Value"];
field = "mainContentPlaceHolder_TextBox" + field.Replace(" ", string.Empty);
ff.FindElement(By.Id(field)).SendKeys(value);
}
}
[When(@"you click submit")]
public void WhenYouClickSubmit()
{
ff.FindElement(By.Id("mainContentPlaceHolder_Submit")).Click();
}
[Then(@"you should see the message ""(.*)""")]
public void ThenYouShouldSeeTheMessage(string expectedMessage)
{
string message = ff.FindElement(By.Id("mainContentPlaceHolder_LabelSuccess")).Text;
Assert.AreEqual(message, expectedMessage);
}
[Then(@"a record should be added to the table")]
public void ThenARecordShouldBeAddedToTheTable()
{
RiskClassesDataContext db = new RiskClassesDataContext();
var query = from ao in db.ActionOwners
where ao.username.Equals(username)
select ao;
Assert.IsNotNull(query.First());
}
}
Questions
I was hoping to be able to use Linq within my step definitions to check that records are being inserted into various tables. The code above is throwing a
NullReferenceException
on the constructor ofRiskClassesDataContext()
. I have been able to create instances of RiskClassesDataContext previously so I'm wondering if this is because I am trying to do this from my Specflow project and not from within my web application.My last question is just whether or not you think this is the best approach for testing my project. Is selenium with database queries okay to test my entire project or would I better off using say Moq. Or maybe both?
Many Thanks