Skip to content

Instantly share code, notes, and snippets.

@johnmmoss
johnmmoss / gist:b61b617a6a3db6547734
Created February 9, 2015 20:49
Mocking the ASP.NET HttpSession with Rhino Mock
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
@johnmmoss
johnmmoss / gist:02d3586d9cc8d36edcb3
Created February 11, 2015 21:16
ASP.NET Uploading and Downloading files
// 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
@johnmmoss
johnmmoss / gist:30b396d2377fb188c08d
Created February 11, 2015 21:20
ASP.NET Razor View for File Upload
// 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>
@johnmmoss
johnmmoss / gist:ee468dbf07e8e7ce96ee
Last active November 9, 2016 14:25
Mocking the ASP.NET IPrincipal with Rhino Mock
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);
@johnmmoss
johnmmoss / gist:5887b9a17b241ebd851c
Last active August 29, 2015 14:15
Mocking the ASP.NET ControllerContext with Rhino Mock
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);
@johnmmoss
johnmmoss / gist:8ed74a2fdc63807518f9
Last active August 29, 2015 14:15
Example Rhino Mock Asserts
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));
@johnmmoss
johnmmoss / gist:18fcfd1a880b4ffe1ed4
Last active August 29, 2015 14:15
Rhino Mock Async Considerations
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:
@johnmmoss
johnmmoss / gist:f69beea8c4911f4f1d5f
Created February 15, 2015 12:52
Rhino Mock Example MVC Asserts
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
@johnmmoss
johnmmoss / gist:0809971957eb425e8f4b
Created February 21, 2015 22:22
Timesheets Application Initial Data Model
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
@johnmmoss
johnmmoss / gist:82a7a8fc9988e10ea693
Created February 22, 2015 11:44
Timesheets App Sample Context
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; }
}