再次運行測試,看見綠條,測試通過。不過這里只是一個假實現并沒有真正實現查找功能,我們需要對測試進行修改使之更加完善。
IE ie = new IE("http://localhost:1781/Default.aspx");
ie.TextField(Find.ById("tb_customerID")).TypeText("ALFKI");
ie.Button(Find.ById("btn_find_customer")).Click();
Assert.That(ie.ContainsText("ALFKI"), Is.True);
Assert.That(ie.ContainsText("Alfreds Futterkiste"), Is.True);
ie.TextField(Find.ById("tb_customerID")).TypeText("AROUT");
ie.Button(Find.ById("btn_find_customer")).Click();
Assert.That(ie.ContainsText("AROUT"), Is.True);
Assert.That(ie.ContainsText("Around the Horn"), Is.True);
ie.Close();
運行測試,又會出現紅條了,測試失敗,F在要考慮實現一個真正的在數據庫中的查找功能,怎么開始做呢?當然還是由測試開始,有了上面的基礎,現在寫的測試跨庫可以稍微大點:
[TestFixture]
public class CustomerDAOTests
{
[Test]
public void ShouldFoundCustomerByID()
{
string id = "ALFKI";
string comName = "Alfreds Futterkiste";
CustomerDAO customerDAO = new CustomerDAO();
Customer found = customerDAO.FindCustomerByID(id);
Assert.That(found, Is.Not.Null);
Assert.That(found.CustomerID, Is.EqualTo(id));
Assert.That(found.CompanyName, Is.EqualTo(comName));
id = "AROUT";
comName = "Around the Horn";
found = customerDAO.FindCustomerByID(id);
Assert.That(found, Is.Not.Null);
Assert.That(found.CustomerID, Is.EqualTo(id));
Assert.That(found.CompanyName, Is.EqualTo(comName));
}
}
這段代碼不能編譯,因為并沒有CustomerDAO這個類,所以得新增該類以及FindCustomerByID方法,而且上面的測試中已經包括了兩個測試場景,現在可以直接寫實現:
public class CustomerDAO
{
public Customer FindCustomerByID(string id)
{
using (NorthwindDataContext ctx = new NorthwindDataContext())
{
IQueryable<Customer> customers = ctx.Customers.Where(c => c.CustomerID == id);
if (customers.Count() > 0)
return customers.Single();
else
return null;
}
}
}
文章來源于領測軟件測試網 http://www.kjueaiud.com/