Basic MVC3 UnitTest fails to UpdateModel() - c#

It's been a while since I did any MVC work, so I'm hopefully missing something. I'm trying to write a test and controller action to simply Edit a DTO named "Business".
Controller Action:
[HttpPost]
public ActionResult Edit(string id, Business business)
{
try
{
var model = _businessRepository.Get(id);
if (model != null)
{
UpdateModel(model);
if (ModelState.IsValid)
{
_businessRepository.Save(model);
}
else
{
return View(business);
}
}
return RedirectToAction("Index");
}
catch
{
return View();
}
}
Test:
[TestMethod]
public void Edit_Post_Action_Updates_Model_And_Redirects()
{
// Arrange
var mockBusinessRepository = new Mock<IBusinessRepository>();
var model = new Business { Id = "1", Name = "Test" };
var expected = new Business { Id = "1", Name = "Not Test" };
// Set up result for business repository
mockBusinessRepository.Setup(m => m.Get(model.Id)).Returns(model);
mockBusinessRepository.Setup(m => m.Save(expected)).Returns(expected);
var businessController = new BusinessController(mockBusinessRepository.Object);
// Act
var result = businessController.Edit(model.Id, expected) as RedirectToRouteResult;
// Assert
Assert.IsNotNull(result);
Assert.AreEqual(result.RouteValues["action"], "Index");
mockBusinessRepository.VerifyAll();
}
The line that it is giving an exception on, is the UpdateModel() in the controller. The exception details are:
"Value cannot be null. Parameter name: controllerContext"

I have some code on Gist that I typically use to set up that ControllerContext. The code is a modified version that was originally taken from Hanselman's blog.
https://gist.github.com/1578697 (MvcMockHelpers.cs)

Setup the Controller context
The following is code snippet from a project which I work on, so maybe it's to much for you
public class TestBase
{
internal Mock<HttpContextBase> Context;
internal Mock<HttpRequestBase> Request;
internal Mock<HttpResponseBase> Response;
internal Mock<HttpSessionStateBase> Session;
internal Mock<HttpServerUtilityBase> Server;
internal GenericPrincipal User;
public void SetContext(Controller controller)
{
Context = new Mock<HttpContextBase>();
Request = new Mock<HttpRequestBase>();
Response = new Mock<HttpResponseBase>();
Session = new Mock<HttpSessionStateBase>();
Server = new Mock<HttpServerUtilityBase>();
User = new GenericPrincipal(new GenericIdentity("test"), new string[0]);
Context.Setup(ctx => ctx.Request).Returns(Request.Object);
Context.Setup(ctx => ctx.Response).Returns(Response.Object);
Context.Setup(ctx => ctx.Session).Returns(Session.Object);
Context.Setup(ctx => ctx.Server).Returns(Server.Object);
Context.Setup(ctx => ctx.User).Returns(User);
Request.Setup(r => r.Cookies).Returns(new HttpCookieCollection());
Request.Setup(r => r.Form).Returns(new NameValueCollection());
Request.Setup(q => q.QueryString).Returns(new NameValueCollection());
Response.Setup(r => r.Cookies).Returns(new HttpCookieCollection());
var rctx = new RequestContext(Context.Object, new RouteData());
controller.ControllerContext = new ControllerContext(rctx, controller);
}
}
Then in your tests you can arrange:
//Arrange
SetContext(_controller);
Context.Setup(ctx => ctx.Request).Returns(Request.Object);
If you want to test your method with ModelState errors, add:
_controller.ModelState.AddModelError("Name", "ErrorMessage");

You need to mock the ControllerContext for your BusinessController.
See this question or this one.

I've managed to get what I wanted working by using Automapper instead of the UpdateModel.
I added in my automapper initialization (IPersistable is an interface for all my DTOs):
Mapper.CreateMap<IPersistable, IPersistable>().ForMember(dto => dto.Id, opt => opt.Ignore());
I then changed my controller action to:
[HttpPost]
public ActionResult Edit(string id, Business business)
{
try
{
var model = _businessRepository.Get(id);
if (model != null)
{
Mapper.Map(business, model);
if (ModelState.IsValid)
{
_businessRepository.Save(model);
}
else
{
return View(business);
}
}
return RedirectToAction("Index");
}
catch
{
return View();
}
}
And changed my test to:
[TestMethod]
public void Edit_Post_Action_Updates_Model_And_Redirects()
{
// Arrange
var mockBusinessRepository = new Mock<IBusinessRepository>();
var fromDB = new Business { Id = "1", Name = "Test" };
var expected = new Business { Id = "1", Name = "Not Test" };
// Set up result for business repository
mockBusinessRepository.Setup(m => m.Get(fromDB.Id)).Returns(fromDB);
mockBusinessRepository.Setup(m => m.Save(It.IsAny<Business>())).Returns(expected);
var businessController = new BusinessController(mockBusinessRepository.Object) {ControllerContext = new ControllerContext()};
//Act
var result = businessController.Edit(fromDB.Id, expected) as RedirectToRouteResult;
// Assert
Assert.IsNotNull(result);
Assert.AreEqual(result.RouteValues["action"], "Index");
mockBusinessRepository.VerifyAll();
}

I had the same problem and used the stack trace to pin this down to the ValueProvider. Building on Andrew's answer above for mocking some of the underlying objects used by a controller, I managed to solve the null value exception by also mocking the ValueProvider like this:
var controller = new MyController();
// ... Other code to mock objects used by controller ...
var mockValueProvider = new Mock<IValueProvider>();
controller.ValueProvider = mockValueProvider.Object;
// ... rest of unit test code which relies on UpdateModel(...)

Related

How do you unit test HTTP Response/Requests in c# controller?

I am trying to unit test a c# controller method. I have looked up many questions and nothing quite gives me instructions on how to unit test the API controller. The code that is correct is below for the method. How do you unit test for the brackets.
I have figured out how to test the method "Reverse Payment", but I am using moq and xunit to test the actual c# method. This is my current unit test:
public class PaymentsControllerTests : BackOfficeIntegrationTestBase
{
private readonly CurrentDateProvider _currentDateProvider;
[BackOfficeRolesAuthorize(BackOfficeUserRole.Admin, BackOfficeUserRole.CSR, BackOfficeUserRole.DealerSupportRep,
BackOfficeUserRole.CSRManager)]
[Fact]
public async Task AdminReversalPermissions()
{
IFixture fixture = new Fixture().Customize(new AutoMoqCustomization());
var mediator = fixture.Freeze<Mock<IMediator>>();
var currentDateProvider = fixture.Freeze<CurrentDateProvider>();
var mockRepo = new Mock<IMediator>();
var controller = new PaymentsController(mockRepo.Object);
// Setup Dependencies
//DataContext dataContext = SetUpDatabase(fixture);
ReversePaymentSetup(fixture, mediator);
var result = await controller.ReversePayment(LeaseInfo.ApplicationId, 100);
var viewResult = result as NoContentResult;
Assert.NotNull(viewResult);
}
private void ReversePaymentSetup(IFixture fixture, Mock<IMediator> mediator)
{
PaymentData data = new PaymentData();
//Mock the mediator
var paymentPlan = new Data.Model.PaymentPlan()
{
LeaseId = 1,
LeaseTerm = LeaseTerm.Year,
DefermentDays = 5,
FirstPaymentDate = DateTime.ParseExact("01/01/2019", "d", CultureInfo.InvariantCulture),
StoreState = "VA"
};
var responseData = new BuildPaymentPlanResponse();
responseData.Data = new BuildPaymentPlanResponse.InitializePaymentPlanResponseData()
{
PaymentPlan = paymentPlan
};
PaymentStatusChangeManualRequest request = fixture.Build<PaymentStatusChangeManualRequest>()
.With(x => x.PaymentPlanId, LeaseInfo.ApplicationId)
.With(x => x.PaymentId, 100)
.With(x => x.Status, PaymentStatus.Reversed)
.Create();
Assert.Equal(PaymentStatus.Reversed, request.Status);
Assert.Equal(100, request.PaymentId);
Assert.NotNull(LeaseInfo.ApplicationId);
}
}
}
[ProducesResponseType(typeof(ErrorResponse), (int)HttpStatusCode.NotFound)]
[HttpPut("{paymentId}/ReversePayment")]
[BackOfficeRolesAuthorize(BackOfficeUserRole.Admin, BackOfficeUserRole.CSR, BackOfficeUserRole.DealerSupportRep,
BackOfficeUserRole.CSRManager)]
public async Task<IActionResult> ReversePayment(int paymentPlanId, int paymentId)
{
await _mediator.Send(new PaymentStatusChangeManualRequest
{
PaymentPlanId = paymentPlanId,
PaymentId = paymentId,
Status = PaymentStatus.Reversed
});
return NoContent();
}

Automapper in xUnit testing and .NET Core 2.0

I have .NET Core 2.0 Project which contains Repository pattern and xUnit testing.
Now, here is some of it's code.
Controller:
public class SchedulesController : Controller
{
private readonly IScheduleRepository repository;
private readonly IMapper mapper;
public SchedulesController(IScheduleRepository repository, IMapper mapper)
{
this.repository = repository;
this.mapper = mapper;
}
[HttpGet]
public IActionResult Get()
{
var result = mapper.Map<IEnumerable<Schedule>, IEnumerable<ScheduleDto>>(source: repository.items);
return new OkObjectResult(result);
}
}
My Test Class:
public class SchedulesControllerTests
{
[Fact]
public void CanGet()
{
try
{
//Arrange
Mock<IScheduleRepository> mockRepo = new Mock<IScheduleRepository>();
mockRepo.Setup(m => m.items).Returns(new Schedule[]
{
new Schedule() { Id=1, Title = "Schedule1" },
new Schedule() { Id=2, Title = "Schedule2" },
new Schedule() { Id=3, Title = "Schedule3" }
});
var mockMapper = new Mock<IMapper>();
mockMapper.Setup(x => x.Map<Schedule>(It.IsAny<ScheduleDto>()))
.Returns((ScheduleDto source) => new Schedule() { Title = source.Title });
SchedulesController controller = new SchedulesController(repository: mockRepo.Object, mapper: mockMapper.Object);
//Act
var result = controller.Get();
//Assert
var okResult = result as OkObjectResult;
Assert.NotNull(okResult);
var model = okResult.Value as IEnumerable<ScheduleDto>;
Assert.NotNull(model);
}
catch (Exception ex)
{
//Assert
Assert.False(false, ex.Message);
}
}
}
Issue I Am facing.
My Issue is that when I run this code with database context and execute Get() method, it works fine, it gives me all results.
But when I tries to run test case, it's not returning any data of Dto object.
When I debugged I found that
I am getting my test object in controller using mockRepo.
But it looks like Auto mapper is not initialized correctly, because while mapping it's not returning anything in
var result = mapper.Map<IEnumerable<Schedule>, IEnumerable<ScheduleDto>>(source: repository.items);
What I tried So Far?
I followed all this answers but still it's not working.
Mocking Mapper.Map() in Unit Testing
How to Mock a list transformation using AutoMapper
So, I need help from someone who is good in xUnit and automapper, and need guidance on how to initialize mock Mapper correctly.
Finally it worked for me, I followed this way How to Write xUnit Test for .net core 2.0 Service that uses AutoMapper and Dependency Injection?
Here I am posting my answer and Test Class so if needed other SO's can use.
public class SchedulesControllerTests
{
[Fact]
public void CanGet()
{
try
{
//Arrange
//Repository
Mock<IScheduleRepository> mockRepo = new Mock<IScheduleRepository>();
var schedules = new List<Schedule>(){
new Schedule() { Id=1, Title = "Schedule1" },
new Schedule() { Id=2, Title = "Schedule2" },
new Schedule() { Id=3, Title = "Schedule3" }
};
mockRepo.Setup(m => m.items).Returns(value: schedules);
//auto mapper configuration
var mockMapper = new MapperConfiguration(cfg =>
{
cfg.AddProfile(new AutoMapperProfile());
});
var mapper = mockMapper.CreateMapper();
SchedulesController controller = new SchedulesController(repository: mockRepo.Object, mapper: mapper);
//Act
var result = controller.Get();
//Assert
var okResult = result as OkObjectResult;
if (okResult != null)
Assert.NotNull(okResult);
var model = okResult.Value as IEnumerable<ScheduleDto>;
if (model.Count() > 0)
{
Assert.NotNull(model);
var expected = model?.FirstOrDefault().Title;
var actual = schedules?.FirstOrDefault().Title;
Assert.Equal(expected: expected, actual: actual);
}
}
catch (Exception ex)
{
//Assert
Assert.False(false, ex.Message);
}
}
}
I needed to inject IMapper, use ProjectTo to get a mapped IQueryable, then implement some more logic on the queryable after the map. So here's what I did to mock it:
var models = new object[]
{
⋮
}.AsQueryable();
var mapper = new Mock<IMapper>();
mapper.Setup(x => x.ProjectTo(
It.IsAny<IQueryable>(),
It.IsAny<object>(),
It.IsAny<Expression<Func<object, object>>[]>()))
.Returns(models);

ASP MVC Unit Testing GetUserId()

I have a controller, where I'm calling a userId:
var userId = User.Identity.GetUserId();
This makes my unit test fail, since User is null.
I've tried to set a user in the test method using this method, but User is still null in the controller when the test runs.
var context = new Mock<HttpContextBase>();
var mockIdentity = new Mock<IIdentity>();
context.SetupGet(x => x.User.Identity).Returns(mockIdentity.Object);
mockIdentity.Setup(u => u.Name).Returns("test_userName");
Any hints as to what I'm doing wrong?
The HttpContextBase.User is of type IPrincipal and you have not mocked it. That's why it is returning null when you access it in tests. You can mock it as follows
var controllerContext = new Mock<ControllerContext>();
var principal = new Moq.Mock<IPrincipal>();
principal.Setup(p => p.IsInRole("Administrator")).Returns(true);
principal.SetupGet(x => x.Identity.Name).Returns(userName);
controllerContext.SetupGet(x => x.HttpContext.User).Returns(principal.Object);
controller.ControllerContext = controllerContext.Object;
Here is the reference of this code
Okay, so according to this post, you can do it like this:
public class MyController: Controller
{
public Func<string> GetUserId; //For testing
public MyController()
{
GetUserId = () => User.Identity.GetUserId();
}
//controller actions
}
And then instead of caling User.Identity.GetUserId() when you want the user, you just call GetUserId() and mock out the user id in your test:
controller = new MyController()
{
GetUserId = () => "IdOfYourChoosing"
};
You are mocking HttpContextBase but are not passing it to your controller.
Try this pattern:
Class:
public MyClass
{
private readonly HttpContextBase _contextBase;
public MyClass(HttpContextBase contextBase)
{
this._contextBase = contextBase;
}
public void Process()
{
var userId = _contextBase.User.Identity;
}
}
Test:
[Test]
public void MyClass_Test_SO()
{
// arrange
var context = new Mock<HttpContextBase>();
var mockIdentity = new Mock<IIdentity>();
context.SetupGet(x => x.User.Identity).Returns(mockIdentity.Object);
mockIdentity.Setup(u => u.Name).Returns("test_userName");
var sut = new MyClass(context.Object);
// act
sut.Process();
// assert
// ... whatever
}

How can use Microsoft Fakes to mock User.Identity.Name

How can I use Microsoft Fakes to mock User.Identity.Name when unit testing MVC 4 application with Visual Studio 2012.
I'm writing unit test for item create action method.
[HttpPost]
public ActionResult Create([Bind(Include = "Name")]Category category)
{
if (categoryService.IsNameExists(category.Name))
{
ModelState.AddModelError("Name", "Category name already exists!");
return View(category);
}
try
{
if (ModelState.IsValid)
{
UserProfile p = new UserProfile();
p.UserName = User.Identity.Name;
category.CreatedBy = p;
category.CreatedDate = DateTime.Now;
category.Active = true;
category.DeletedBy = null;
category = categoryService.SaveCategory(category);
return RedirectToAction("Index");
}
return View(category);
}
catch (DataException dex)
{
}
}
[TestMethod]
public void Create()
{
Category createdCategory = new Category();
ICategoryService service = new StubICategoryService()
{
SaveCategoryCategory = (category) => { return category; }
};
CategoryController controller = new CategoryController(service);
using (ShimsContext.Create())
{
System.Fakes.ShimDateTime.NowGet = () =>
{ return new DateTime(2000, 1, 1); };
ViewResult result = controller.Create(createdCategory) as ViewResult;
Assert.IsNotNull(result);
}
}
These are the action method and test method I have written. If there is better way to do this other than MS Fakes please tell me, (not another mocking framework).
Assuming you've added Fakes references for System.Web and System, you can do something like this inside your using (ShimsContext.Create()) block:
var context = new System.Web.Fakes.ShimHttpContext();
var user = new StubIPrincipal
{
IdentityGet = () =>
{
var identity = new StubIIdentity {NameGet = () => "foo"};
return identity;
}
};
context.UserGet = () => principal;
System.Web.Fakes.ShimHttpContext.CurrentGet = () => { return context; };
User is actually HttpContext.User. So you could use System.Fakes.ShimHttpContext to return a custom implementation of the whole IPrincipal containing the right Identity.Name...
I ended up with a similar answer to #Sven, but ended up stubbing the context instead of using a shim.
using (AccountController controller = new AccountController())
{
StubHttpContextBase stubHttpContext = new StubHttpContextBase();
controller.ControllerContext = new ControllerContext(stubHttpContext, new RouteData(), controller);
StubIPrincipal principal = new StubIPrincipal();
principal.IdentityGet = () =>
{
return new StubIIdentity
{
NameGet = () => "bob"
};
};
stubHttpContext.UserGet = () => principal;
}

How to mock HttpContext in a ShoppingCart controller/model

In our MVC4 application with Entity Framework 4.0 based on the Music Store Tutorial we are using Moq to mock the DbContext and unit test are logic. One of our methods proves difficult to test though since it makes use of HttpContext or HttpContextBase. One example method looks like this:
public static ShoppingCart GetCart(HttpContextBase context)
{
var cart = new ShoppingCart();
cart.ShoppingCartId = cart.GetCartId(context);
return cart;
}
The only property collected from HttpContextBase is the [CartSessionKey] as can be seen here:
public string GetCartId(HttpContextBase context)
{
if (context.Session[CartSessionKey] == null)
{
if (!string.IsNullOrWhiteSpace(context.User.Identity.Name))
{
context.Session[CartSessionKey] =
context.User.Identity.Name;
}
else
{
// Generate a new random GUID using System.Guid class
Guid tempCartId = Guid.NewGuid();
// Send tempCartId back to client as a cookie
context.Session[CartSessionKey] = tempCartId.ToString();
}
}
return context.Session[CartSessionKey].ToString();
}
We have heard horror stories that HttpContext is a very complex class and that if you print it you have enough paper to circle the earth eight times.
Nevertheless we want to mock it. The question is how. The properties that we want to mock are the [CartSessionKey], and the property that come from the context as contest.User.Identity.Name.
We suspect we need to use something like this:
var mockData = new Mock<FakeContext>();
mockData.Setup(m => m.Orders).Returns(memoryOrderItems);
mockData.Setup(m => m.Carts).Returns(memoryCartItems);
Mock<HttpContextBase> mockHttpContext = new Mock<HttpContextBase>();
Mock<HttpRequestBase> mockHttpRequest = new Mock<HttpRequestBase>();
mockHttpRequest.Setup(x => x.CartSessionKey).Returns(1);
mockHttpContext.Setup(x => x.Request).Returns(mockHttpRequest.Object);
but we cannot find how to specifically implement this so we do not get any errors on methods that use context.Session[CartSessionKey] or context.User.Identity.Name.
We hope someone can help us out.
/edit
When we do this:
var memoryUserItems = new FakeDbSet<User>()
{
new User { Email = "test#test.de",
FullName = "Test Person",
isAvailable = true,
Name = "WHat"
},
new User { Email = "test2#test.de",
FullName = "Test Person 2",
isAvailable = true,
Name = "WHat 2"
}
};
(...) Other memory...Items
And then this:
// Create mock units of work
var mockData = new Mock<FakeContext>();
mockData.Setup(m => m.Orders).Returns(memoryOrderItems);
mockData.Setup(m => m.Carts).Returns(memoryCartItems);
mockData.Setup(m => m.Users).Returns(memoryUserItems);
var principalMock = new Mock<IPrincipal>();
var identityMock = new Mock<IIdentity>();
var userMock =
identityMock.Setup(x => x.Name).Returns("Test!");
identityMock.Setup(x => x.IsAuthenticated).Returns(true); // optional ;)
mockData.Setup(x => x.Identity).Returns(identityMock.Object);
var httpReqBase = new Mock<HttpRequestBase>(); // this is useful if you want to test Ajax request checks or cookies in the controller.
var httpContextBase = new Mock<HttpContextBase>();
httpContextBase.Setup(x => x.User).Returns(principalMock.Object);
httpContextBase.Setup(x => x.Session[It.IsAny<string>()]).Returns(1); //Here is the session indexer. You can swap 'any' string for specific string.
httpContextBase.Setup(x => x.Request).Returns(httpReqBase.Object);
We get the error that:
Error 3 'project.Models.FakeContext' does
not contain a definition for 'Identity' and no extension method
'Identity' accepting a first argument of type
'project.Models.FakeContext' could be found
(are you missing a using directive or an assembly
reference?)
/ edit2
To make it more clear. The actual method I am testing is the following:
public ActionResult Complete(int id)
{
// Make sure that user is currentuser and otherwise bring user to our Thief page
if (id != db.GetCurrentUserId())
{
return View("Thief");
}
var cart = ShoppingCart.GetCart(this.HttpContext);
var currentDate = DateTime.Today;
var viewModel = new ShoppingCartViewModel
{
CartItems = cart.GetCartItems(),
CartTotal = cart.GetTotal(),
ProductItems = db.Products.ToList()
};
if (viewModel.CartItems.Count() == 0)
{
return View("Empty");
}
// Try to write cart to order table
try
{
foreach (var item in viewModel.CartItems)
{
ProcessOrder(item, id, currentDate);
}
// after this we empty the shopping cart
cart.EmptyCart();
return View();
}
catch
{
// Invalid - display error page
return View("Error");
}
}
As can be seen the var cart = ShoppingCart.GetCart(this.HttpContext); uses this.HttpContext. In the test I just do controller.Complete(1). I cannot pass a new HttpContext to the controller I guess?
/ edit 3
While using the code below with the mocks I get the following message:
Test Name: TestCheckoutCompleteShouldWithEmptyCart
Test FullName: Controllers.CheckoutControllerTest.TestCheckoutCompleteShouldWithEmptyCart
Test Source: Controllers\CheckoutControllerTest.cs : line 141
Test Outcome: Failed
Test Duration: 0:00:00.0158591
Result Message:
Test method Controllers.CheckoutControllerTest.TestCheckoutCompleteShouldWithEmptyCart threw exception:
System.NullReferenceException: Object reference not set to an instance of an object.
Result StackTrace:
at Models\ShoppingCart.cs:line 170
at \Models\ShoppingCart.cs:line 20
at \Controllers\CheckoutController.cs:line 48
at Controllers\CheckoutControllerTest.cs:line 143
OK, here it goes. The following works in MVC5 with AD, I'm not sure if it's fully backwards compatible, you'll have to check.
var principalMock = new Mock<IPrincipal>();
var identityMock = new Mock<IIdentity>();
identityMock.Setup(x => x.Name).Returns("Test!");
identityMock.Setup(x => x.IsAuthenticated).Returns(true); // optional ;)
userMock.Setup(x => x.Identity).Returns(identityMock.Object);
var httpReqBase = new Mock<HttpRequestBase>(); // this is useful if you want to test Ajax request checks or cookies in the controller.
var httpContextBase = new Mock<HttpContextBase>();
httpContextBase.Setup(x => x.User).Returns(principalMock.Object);
httpContextBase.Setup(x => x.Session[It.IsAny<string>()]).Returns(1); //Here is the session indexer. You can swap 'any' string for specific string.
httpContextBase.Setup(x => x.Request).Returns(httpReqBase.Object);
This would help you to write a proper Unit Test using Moq.
[TestClass]
public class SutTest
{
[TestMethod]
public void GetCartId_WhenUserNameIsNotNull_SessionContainsUserName()
{
var httpContextStub = new Mock<HttpContextBase>();
var httpSessionStub = new Mock<ISessionSettings>();
httpSessionStub.Setup(x => x.Get<string>(It.IsAny<string>())).Returns(() => null);
httpSessionStub.SetupSequence(x => x.Get<string>(It.IsAny<string>()))
.Returns(null)
.Returns("FakeName");
var httpUserStub = new Mock<IPrincipal>();
var httpIdenttyStub = new Mock<IIdentity>();
httpUserStub.SetupGet(x => x.Identity).Returns(httpIdenttyStub.Object);
httpIdenttyStub.SetupGet(x => x.Name).Returns("FakeName");
httpContextStub.Setup(x => x.User).Returns(httpUserStub.Object);
var sut = new Sut(httpSessionStub.Object);
var result = sut.GetCartId(httpContextStub.Object);
Assert.AreEqual("FakeName",result );
}
}
Check the SetupSequence method which gives you find Control over different values being return on he same stubbed call.
Also important to decouple your session from HttpContext as you can always run into issues.
public class SessionSettings : ISessionSettings
{
private readonly HttpSessionStateBase _session;
public SessionSettings(HttpSessionStateBase session)
{
_session = session;
}
public T Get<T>(string key)
{
return (T)_session[key];
}
public void Set<T>(string key, T value)
{
_session[key] = value;
}
}
public interface ISessionSettings
{
T Get<T>(string key);
void Set<T>(string key, T value);
}
public class Sut
{
private ISessionSettings _sessionSettings;
public Sut(ISessionSettings sessionSettings)
{
_sessionSettings = sessionSettings;
}
public string GetCartId(HttpContextBase context)
{
if (_sessionSettings.Get<string>(CartSessionKey) == null)
{
if (!string.IsNullOrWhiteSpace(context.User.Identity.Name))
{
_sessionSettings.Set<string>(CartSessionKey, context.User.Identity.Name);
}
else
{
// Generate a new random GUID using System.Guid class
Guid tempCartId = Guid.NewGuid();
// Send tempCartId back to client as a cookie
_sessionSettings.Set<string>(CartSessionKey, tempCartId.ToString());
}
}
return _sessionSettings.Get<string>(CartSessionKey);
}
private string CartSessionKey = "key";
}
This way the code is more readable and easier to understand.

Categories