1

我们正在尝试在我们的新项目中结合 BDD 和 TDD 方法。

在我们的 MVC3 项目中使用 Specflow、NUnit 和 WatiN,我试图遵循由外而内流程的逻辑。http://msdn.microsoft.com/en-us/magazine/gg490346.aspx 我想知道我正在尝试做的是正确的。

Feature: Commit RPI, As any user, I want to commit all RPI values

Scenario: Commit RPI values, Given I am in the RPI screen And I have entered RPI values

| Year | Month | RPI Value |    
| 2012 | 1     | 1.2       |

当我想提交 RPI 值时,应该提交 RPI 值

验收测试如下:

[Given(@"I am in the RPI screen")]    
public void GivenIAmInTheRPIScreen()    
{    
    WebBrowser.Current.GoTo("http://localhost:8010/");    //WebBrowser is the instance of Internet Explorer created by WatiN
    Assert.AreEqual(WebBrowser.Current.Title, "RPI List");
}

***This step will initially fail as there will not be any RPI List screen. So, I’ll go to the web project, create a controller and view. When I run the test again, WatiN will be able to open up the screen and pass the test.***

[Given(@"I have entered RPI values")]    
public void GivenIHaveEnteredRPIValues(Table tblRPI)
{    
    if (tblRPI.Rows.Count > 0)   
    {
        var txtYear = (TextField) WebBrowser.Current.Elements.First(Find.ById("txtRPIYear"));

        if (txtYear != null) 
           txtYear.Value = tblRPI.Rows[0]["Year"];

        var txtMonth = (TextField)WebBrowser.Current.Elements.First(Find.ById("txtRPIMonth"));

        if (txtMonth != null)
            txtMonth.Value = tblRPI.Rows[0]["Month"];

        var txtValue = (TextField)WebBrowser.Current.Elements.First(Find.ById("txtRPIValue"));

        if (txtValue != null)
            txtValue.Value = tblRPI.Rows[0]["RPI Value"];
    }
    else    
    {
        Assert.True(false);
    }
}

[When(@"I want to commit RPI values")]    
public void WhenIWantToCommitRPIValues()
{

   var btnCommitValues = (Button)WebBrowser.Current.Elements.First(Find.ById("btnCommitValues"));

   if (btnCommitValues != null)
   {
       btnCommitValues.Focus();
   }
   else
   {
       Assert.IsTrue(btnCommitValues != null);
   }
}

[Then(@"RPI values should be committed")]
public void ThenRPIValuesShouldBeCommitted()
{
    var btnCommitValues = (Button)WebBrowser.Current.Elements.First(Find.ById("btnCommitValues"));

    if (btnCommitValues != null)
    {
        btnCommitValues.Click();
    }
    else
    {
        Assert.IsTrue(btnCommitValues != null);    
    }
}

此步骤将失败,因为不会有任何逻辑来提交值。在这个阶段,我将停止 BDD 并继续为提交逻辑编写我的单元测试用例。我将使用 NUnit 编写各种测试用例,让它们通过,这样当我回到验收测试时,就会有一个逻辑来提交已经通过我的单元测试的 RPI 值,现在验收测试也将通过。

我的问题几乎与此类似How do specflow features link to unit tests in asp.net webforms projects?

如果您使用 SpecFlow、NUnit 和 MVC 做过类似的工作,请分享您的想法。.

4

0 回答 0