Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mocking a TempData in ASP.NET Core in MSTest

public ActionResult View(string name)
{
    if (TempData["SessionVariable"] != null)
    {
        FileName = name;  
        return View();
    }
    else
    {  
        return RedirectToAction("index", "Home");
    }

}

TestMethod

public void UseCaseView_CorrectRequirements()
{
    var mock = new Mock<Controller>();
    mock.Setup(p => p.TempData["SessionVariable"]).Returns("admin");
    Controller.View("SAMPLE.xml");           
}

It throws an error such as ,

Result StackTrace:

at Moq.Mock.ThrowIfSetupExpressionInvolvesUnsupportedMember(Expression setup, MethodInfo method) in C:\projects\moq4\Source\Mock.cs:line 883 at Moq.Mock.SetupGetPexProtected[T,TProperty](Mock1 mock, Expression1 expression, Condition condition) in C:\projects\moq4\Source\Mock.cs:line 537 at Moq.Mock.SetupGet[T,TProperty](Mock1 mock, Expression1 expression, Condition condition) in C:\projects\moq4\Source\Mock.cs:line 517 at Moq.Mock.SetupPexProtected[T,TResult](Mock1 mock, Expression1 expression, Condition condition) in C:\projects\moq4\Source\Mock.cs:line 507 at Moq.Mock.Setup[T,TResult](Mock1 mock, Expression1 expression, Condition condition) in C:\projects\moq4\Source\Mock.cs:line 484 at Moq.QueryableMockExtensions.FluentMock[T,TResult](Mock1 mock, Expression1 setup) in C:\projects\moq4\Source\Linq\Mocks.cs:line 224 at lambda_method(Closure ) at Moq.Mock.GetTargetMock(Expression fluentExpression, Mock mock) in C:\projects\moq4\Source\Mock.cs:line 856 at Moq.Mock.SetupPexProtected[T,TResult](Mock1 mock, Expression1 expression, Condition condition) in C:\projects\moq4\Source\Mock.cs:line 505 at Moq.Mock.Setup[T,TResult](Mock1 mock, Expression1 expression, Condition condition) in C:\projects\moq4\Source\Mock.cs:line 484 at SE_WEBAPP.Tests.TestControllers.UsecaseControllerTests.UseCaseView_CorrectRequirements() in

D:\Public\SE_WEBAPP_TESTCASE\SE_WEBAPP\SE_WEBAPP.Tests\TestControllers\UsecaseControllerTests.cs:line 23

Result Message: Test method SE_WEBAPP.Tests.TestControllers.UsecaseControllerTests.UseCaseView_CorrectRequirements threw exception: System.NotSupportedException: Invalid setup on a non-virtual (overridable in VB) member: mock => mock.TempData

like image 816
Sivabalakrishnan Avatar asked Sep 05 '18 09:09

Sivabalakrishnan


People also ask

Does MSTest work with .NET core?

For MSTest before version 2.2. 4, the timeout is used for all test cases. This option is supported on Windows with netcoreapp2.

Can we use TempData in .NET core?

TempData. ASP.NET Core exposes the Razor Pages TempData or Controller TempData. This property stores data until it's read in another request. The Keep(String) and Peek(string) methods can be used to examine the data without deletion at the end of the request.

How do I pass TempData to view?

To pass the strongly typed data from Controller to View using TempData, we have to make a model class then populate its properties with some data and then pass that data to TempData as Value and selecting Key's name is the programmer's choice.


1 Answers

In asp.net-core, Controller.TempData is a public property, so you can easily access it and set the desired key/value

public void UseCaseView_CorrectRequirements() {
    // Arrange
    var httpContext = new DefaultHttpContext();
    var tempData = new TempDataDictionary(httpContext, Mock.Of<ITempDataProvider>());
    tempData["SessionVariable"] = "admin";        
    var controller = new UsecaseController() {
        TempData = tempData
    };
    var expected = "SAMPLE.xml";

    //Act
    var view = controller.View(expected) as ViewResult;
    var actual = controller.FileName;

    //Assert
    Assert.AreEqual(expected, actual);
}
like image 55
Nkosi Avatar answered Oct 24 '22 18:10

Nkosi