user2842476
user2842476

Reputation: 13

How to write a Unittest case to check the return view?

 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
        }

I write a test case for this code.but it's not work it gives the error

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'.

my test method is

[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);


} 

please help me to solve this or help me to write a test case for that method..

Upvotes: 0

Views: 105

Answers (1)

EagleBeak
EagleBeak

Reputation: 7419

If you are serious about unit testing this, I think you should wrap the data context in a mockable class and mock that in your test using a mocking framework like NSubstitute. Otherwise your test will probably try to hit some database, which is inappropriate for a unit test.

Upvotes: 2

Related Questions