再次运行测试,看见绿条,测试通过。不过这里只是一个假实现并没有真正实现查找功能,我们需要对测试进行修改使之更加完善。
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();
运行测试,又会出现红条了,测试失败。现在要考虑实现一个真正的在数据库中的查找功能,怎么开始做呢?当然还是由测试开始,有了上面的基础,现在写的测试跨库可以稍微大点:
[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;
}
}
}
文章来源于领测软件测试网 https://www.ltesting.net/