Created
September 8, 2012 14:00
-
-
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
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 interface IView | |
| { | |
| bool IsPostBack { get; } | |
| bool IsValid { get; } | |
| void DataBind(); | |
| void DisplayErrorMessage(string message); | |
| } |
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 interface IProductList : IView | |
| { | |
| IEnumerable<ProductCategoryDTO> ProductCategories { get; set; } | |
| IEnumerable<ProductOverviewDTO> Products { get; set; } | |
| IEnumerable<SupplierDTO> Suppliers { get; set; } | |
| } |
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 abstract class Controller<T> : Disposable, IController where T : IView | |
| { | |
| protected Controller(T view) | |
| { | |
| View = view; | |
| } | |
| protected T View { get; set; } | |
| } |
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 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; | |
| } | |
| } |
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 interface IProductsNavigator | |
| { | |
| void GoToEdit(int? productId); | |
| void GoToSearch(); | |
| } |
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 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)); | |
| } | |
| } |
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
| 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; | |
| } |
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
| [Test] | |
| public void DoesNotRetrieveCategoriesAndSuppliersOnLoadIfPostBack() | |
| { | |
| view.Stub(v => v.IsPostBack).Return(true); | |
| mocks.ReplayAll(); | |
| CreateController().Load(); | |
| service.AssertWasNotCalled(s => s.Process(null), options => options.IgnoreArguments()); | |
| } |
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
| [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