This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void SetUpSession() | |
{ | |
// Arrange | |
var httpContext = MockRepository.GenerateStub<HttpContextBase>(); | |
var httpSession = MockRepository.GenerateStub<HttpSessionStateBase>(); | |
httpContext.Stub(h => h.Session).Return(httpSession); | |
mock.Setup(p => p.HttpContext.Session).Returns(mockSession.Object); | |
// Now create the system under test |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Sample model uses HttpPostedFileBase to pass the file to the controller | |
public class CustomerStatementModel | |
{ | |
public int Id { get; set; } | |
public string Description { get; set; } | |
public HttpPostedFileBase CustomerStatementFile { get; set; } | |
} | |
// POST:/Customer/Statement | |
// To upload a customer statment post to this action |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// The view needs to have the enctype set to multipart form data, also, TextBoxFor takes type of file | |
@using (Html.BeginForm("Statement", "Customer", FormMethod.Post, new { enctype = "multipart/form-data" })) | |
{ | |
// Sample Razor | |
<div class="form-group"> | |
@Html.LabelFor(m => m.CustomerStatementFile, new { @class = "control-label" }) | |
<div class="col-sm-10"> | |
@Html.TextBoxFor(m => m.CustomerStatementFile, new { @class = "form-control", type = "file" }) | |
</div> | |
</div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void SetUpHttpRequestUser() | |
{ | |
// Setup the controller context plumbing | |
var httpContext = MockRepository.GenerateStub<HttpContextBase>(); | |
var identity = MockRepository.GenerateStub<IIdentity>(); | |
var principal = MockRepository.GenerateStub<IPrincipal>(); | |
// Set the required test values | |
identity.Stub(i => i.Name).Return("TestUser"); | |
principal.Stub(u => u.Identity).Return(identity); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void SetUpControllerContext() | |
{ | |
// Setup the controller context plumbing | |
var httpContext = MockRepository.GenerateMock<HttpContextBase>(); | |
var httpRequest = MockRepository.GenerateMock<HttpRequestBase>(); | |
var httpResponse = MockRepository.GenerateMock<HttpReponseBase>(); | |
var browserMock = MockRepository.GenerateMock<HttpBrowserCapabilitiesBase>(); | |
// Set the required test values | |
httpContext.Stub(h => h.Request.Browser).Return(browserMock); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void RhinoMockExampleAsserts() | |
{ | |
// Example assert _principal.IsInRole(..) wasn't called | |
_principal.AssertWasNotCalled(x => x.IsInRole(Arg<string>.Is.Anything)) | |
// Assert API call was made | |
_apiClient.AssertWasCalled(x => x.GetCustomerStatement(Arg<int>.Is.Anything)); | |
// Assert API call was made with a specific ID | |
_apiClient.AssertWasCalled(x => x.GetCustomerStatement(Arg<int>.Is.Equal(200)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void AsyncExamples() | |
{ | |
// Async controller actions have to be called asyn: | |
await controller.Create(model); | |
// Async methods return a task, so use Task.FromResult: | |
_apiClient.Stub(x => x.GetCustomersAsync(Arg<long>.Is.Anything)) | |
.Return(Task.FromResult(new List<Customer>())); | |
// If the method returns just Task and not Task<T> use true/null/1: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void ExampleMvcAsserts() | |
{ | |
// Setup an error in the model so that ModelState.IsValid returns false. | |
// Used to check that the ModelState.IsValid check is in place. | |
controller.ViewData.ModelState.AddModelError("Surname", "The Surname field is required."); | |
// Call the controller method. This usually returns an ActionResult, which may need to be cast. | |
var result = _userController.Create(user).Result; | |
// Assert that the redirect result is of a particular type |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Department | |
{ | |
public int Id { get; set; } | |
public string Code { get; set; } | |
public string Name { get; set; } | |
ICollection<Employee> Employees { get; set; } | |
ICollection<TimeCode> TimeCodes { get; set; } | |
} | |
public class Employee |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class TimesheetsContext : DbContext | |
{ | |
public DbSet<Employee> Employees { get; set; } | |
public DbSet<Department> Departments { get; set; } | |
public DbSet<TimeCode> TimeCodes { get; set; } | |
public DbSet<Timesheet> Timesheets { get; set; } | |
} |
OlderNewer