Skip to content

Instantly share code, notes, and snippets.

@davybrion
Created September 8, 2012 14:00
Show Gist options
  • Select an option

  • Save davybrion/3675170 to your computer and use it in GitHub Desktop.

Select an option

Save davybrion/3675170 to your computer and use it in GitHub Desktop.
Code snippets for "How To Write Testable ASP.NET WebForms" post, part I
public interface IView
{
bool IsPostBack { get; }
bool IsValid { get; }
void DataBind();
void DisplayErrorMessage(string message);
}
public interface IProductList : IView
{
IEnumerable<ProductCategoryDTO> ProductCategories { get; set; }
IEnumerable<ProductOverviewDTO> Products { get; set; }
IEnumerable<SupplierDTO> Suppliers { get; set; }
}
public abstract class Controller<T> : Disposable, IController where T : IView
{
protected Controller(T view)
{
View = view;
}
protected T View { get; set; }
}
public class ProductListController : Controller<IProductList>
{
private IProductManagementService service;
private readonly IProductsNavigator navigator;
public ProductListController(IProductList view, IProductManagementService service,
IProductsNavigator navigator) : base(view)
{
this.service = service;
this.navigator = navigator;
}
protected override void DisposeObjects()
{
if (service != null) service.Dispose();
}
protected override void ClearReferences()
{
View = null;
service = null;
}
}
public interface IProductsNavigator
{
void GoToEdit(int? productId);
void GoToSearch();
}
public abstract class ControllerTest
{
protected static void PrepareServiceToReturnResponses(IService service, ServiceRequestResponseSpy spy,
params Response[] responses)
{
spy.SetResponsesToReturn(responses);
service.Stub(s => s.Process(null))
.IgnoreArguments()
.Do(new Func<Request[], Response[]>(spy.GrabRequestsAndReturnGivenResponses));
}
}
private MockRepository mocks;
private IProductManagementService service;
private IProductList view;
private IProductsNavigator navigator;
private ProductListController controller;
[SetUp]
public void SetUp()
{
mocks = new MockRepository();
service = mocks.DynamicMock<IProductManagementService>();
view = mocks.DynamicMock<IProductList>();
navigator = mocks.DynamicMock<IProductsNavigator>();
}
private ProductListController CreateController()
{
controller = new ProductListController(view, service, navigator);
return controller;
}
[Test]
public void DoesNotRetrieveCategoriesAndSuppliersOnLoadIfPostBack()
{
view.Stub(v => v.IsPostBack).Return(true);
mocks.ReplayAll();
CreateController().Load();
service.AssertWasNotCalled(s => s.Process(null), options => options.IgnoreArguments());
}
[Test]
public void RetrievesCategoriesAndSuppliersOnLoad()
{
var categoriesToReturn = new ProductCategoryDTO[0];
var suppliersToReturn = new SupplierDTO[0];
var spy = new ServiceRequestResponseSpy();
PrepareServiceToReturnResponses(service, spy, new GetProductCategoriesResponse(categoriesToReturn),
new GetSuppliersResponse(suppliersToReturn));
view.Expect(v => v.ProductCategories = categoriesToReturn);
view.Expect(v => v.Suppliers = suppliersToReturn);
view.Expect(v => v.DataBind());
mocks.ReplayAll();
CreateController().Load();
view.VerifyAllExpectations();
Assert.IsNotNull(spy.GetRequest<GetProductCategoriesRequest>());
Assert.IsNotNull(spy.GetRequest<GetSuppliersRequest>());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment