Created
September 8, 2012 14:04
-
-
Save davybrion/3675198 to your computer and use it in GitHub Desktop.
Code snippets for "How To Write Testable ASP.NET WebForms" post, part II
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 Load() | |
| { | |
| if (!View.IsPostBack) | |
| { | |
| var batcher = new ServiceCallBatcher(service); | |
| batcher.Add(new GetProductCategoriesRequest()); | |
| batcher.Add(new GetSuppliersRequest()); | |
| View.ProductCategories = batcher.Get<GetProductCategoriesResponse>().ProductCategories; | |
| View.Suppliers = batcher.Get<GetSuppliersResponse>().Suppliers; | |
| View.DataBind(); | |
| } | |
| } |
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 CallsTheServiceToSearchForProductsAndBindsResultsToView() | |
| { | |
| const string productPattern = "whatever"; | |
| const int categoryId = 4; | |
| const int supplierId = 7; | |
| var productsToReturn = new ProductOverviewDTO[0]; | |
| var spy = new ServiceRequestResponseSpy(); | |
| PrepareServiceToReturnResponses(service, spy, new GetProductOverviewsResponse(productsToReturn)); | |
| view.Expect(v => v.Products = productsToReturn); | |
| view.Expect(v => v.DataBind()); | |
| mocks.ReplayAll(); | |
| CreateController().Search(productPattern, categoryId, supplierId); | |
| view.VerifyAllExpectations(); | |
| var request = spy.GetRequest<GetProductOverviewsRequest>(); | |
| Assert.AreEqual(productPattern, request.NamePattern); | |
| Assert.AreEqual(categoryId, request.CategoryId); | |
| Assert.AreEqual(supplierId, request.SupplierId); | |
| } |
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 Search(string name, int? productCategoryId, int? supplierId) | |
| { | |
| var response = service.GetProductOverviews( | |
| new GetProductOverviewsRequest(name, productCategoryId, supplierId)); | |
| View.Products = response.Products; | |
| View.DataBind(); | |
| } |
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 NavigatesToEditProductScreenWhenEditProductIsTriggered() | |
| { | |
| const int productId = 5; | |
| navigator.Expect(n => n.GoToEdit(productId)); | |
| mocks.ReplayAll(); | |
| CreateController().EditProduct(productId); | |
| navigator.VerifyAllExpectations(); | |
| } |
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 EditProduct(int productId) | |
| { | |
| navigator.GoToEdit(productId); | |
| } |
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 NorthwindPage<T> : Page where T : IController | |
| { | |
| protected NorthwindPage() | |
| { | |
| Controller = CreateController(); | |
| } | |
| protected T Controller { get; private set; } | |
| protected abstract T CreateController(); | |
| protected override void OnPreRenderComplete(EventArgs e) | |
| { | |
| base.OnPreRenderComplete(e); | |
| Controller.Dispose(); | |
| } | |
| // there are also some small helper methods in here that aren't really | |
| // relevate to the example so i left them out | |
| } |
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 partial class ProductList : NorthwindPage<ProductListController>, IProductList | |
| { | |
| public IEnumerable<ProductCategoryDTO> ProductCategories { get; set; } | |
| public IEnumerable<ProductOverviewDTO> Products { get; set; } | |
| public IEnumerable<SupplierDTO> Suppliers { get; set; } | |
| protected void Page_Load(object sender, EventArgs e) | |
| { | |
| Controller.Load(); | |
| } | |
| protected override ProductListController CreateController() | |
| { | |
| return Container.Resolve<ProductListController>(new { view = this }); | |
| } | |
| protected void SearchButton_Click(object sender, EventArgs e) | |
| { | |
| Controller.Search(NameTextBox.Text, GetSelectedId(ProductCategoryList), GetSelectedId(SupplierList)); | |
| } | |
| protected void EditProductLink_Click(object sender, EventArgs e) | |
| { | |
| Controller.EditProduct(GetIdForCurrentRow(sender as IButtonControl)); | |
| } | |
| public override void DataBind() | |
| { | |
| if (ProductCategories != null && Suppliers != null) | |
| { | |
| var categories = new[] { new ProductCategoryDTO { Id = -1, Name = "All" } }.Union(ProductCategories); | |
| var suppliers = new[] { new SupplierDTO { Id = -1, CompanyName = "All" } }.Union(Suppliers); | |
| PrepareDropDownList(ProductCategoryList, categories, "Name", "Id"); | |
| PrepareDropDownList(SupplierList, suppliers, "CompanyName", "Id"); | |
| } | |
| if (Products != null) | |
| { | |
| ProductsGrid.DataSource = Products; | |
| } | |
| base.DataBind(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment