0
 AdvertisementDataContext db = new AdvertisementDataContext(); //make an object so that we can retrieve data from database

        public ActionResult Index()
        {

            var advertisement = db.Advertisements.ToArray(); // retrieve data from database
            return View(advertisement); // we return the object to the index view
        }

我为此代码编写了一个测试用例。但它不起作用,它给出了错误

Error   1   The type 'System.Data.Entity.DbContext' is defined in an assembly that is not referenced. You must add a reference to assembly 'EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.

我的测试方法是

[TestMethod]

public void TestIndex() {
    Bartering.Models.AdvertisementDataContext db = new Bartering.Models.AdvertisementDataContext();
    AdvertisementController controller = new AdvertisementController();
    ViewResult result = controller.Index() as ViewResult;
    Assert.AreEqual(db.Advertisements, result.ViewBag);


} 

请帮我解决这个问题或帮我为该方法编写一个测试用例..

4

1 回答 1

2

如果您对单元测试很认真,我认为您应该将数据上下文包装在一个可模拟的类中,并使用像NSubstitute这样的模拟框架在您的测试中模拟它。否则,您的测试可能会尝试访问某些数据库,这不适合单元测试。

于 2013-10-04T16:47:10.637 回答