環境
私は .NET に比較的慣れていないので、プロジェクトで BDD を使用することにしました。これにはSpecflowを使用しています。
Gherkin 形式を使用して機能ファイルを作成し、ステップ定義を生成しました。
Selenium を使用して機能ファイルのテーブルから Web ページに情報を挿入し、MSTest を使用して結果をテストしています。
私のステップ定義
[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());
}
}
質問
ステップ定義内で Linq を使用して、レコードがさまざまなテーブルに挿入されていることを確認できるようにしたいと考えていました。上記のコードは
NullReferenceException
、 のコンストラクターで をスローしていRiskClassesDataContext()
ます。以前は RiskClassesDataContext のインスタンスを作成できたので、Web アプリケーション内からではなく、Specflow プロジェクトからこれを実行しようとしているからではないかと考えています。最後の質問は、これが私のプロジェクトをテストするための最良のアプローチだと思うかどうかです。データベースクエリを使用したセレンは、プロジェクト全体をテストするのに問題ありませんか、それとも Moq を使用したほうがよいでしょうか。それとも両方?
どうもありがとう