0

I have the following action:

public ViewResult Index() { var model = new MyIndexViewModel { TotalTips = (decimal)7.51 }; return View(model); } 

I would like to write a unit test that verifies that model.TotalTips is 7.51.

I am trying something like this:

[TestMethod] public void Test() { // Arrange var controller = new MyController(_mockRepository.Object); MyIndexViewModel test = new MyIndexViewModel{TotalTips = (decimal)7.51}; // Action ViewResult result = controller.Index(); // Assert Assert.AreEqual(result.ViewData.Model.TotalTips, test.TotalTips); // Problem line } 

But I cannot resolve TotalTips on result.ViewData.Model.TotalTips.

I know this is wrong, but cannot figure out how to properly resolve the model I am posting to the Index view.

1 Answer 1

2

According to the documentation ViewDataDictionary.Model is typed as object, so you'll have to cast it to your view model type:

var viewModel = (MyIndexViewModel)result.ViewData.Model; Assert.AreEqual(viewModel.TotalTips, test.TotalTips); 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.