Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

ASP.NET Core Unit Testing

2,779 views

Published on

Blog post: http://WakeUpAndCode.com/asp-net-core-testing

Learn all about automated unit testing in ASP.NET Core 1.0 (formerly known as ASP.NET 5) and how you can set up Visual Studio so that you can quickly test your apps in the real world.

Published in: Technology
  • Be the first to comment

  • Be the first to like this

ASP.NET Core Unit Testing

  1. 1. Build Robust Web Apps in the Real World @shahedC WakeUpAndCode.com * aka ASP.NET 5 before RC1
  2. 2. Background > Getting Started > Writing and Running Tests > Dependency Injection & Mocking What’s Next?
  3. 3. > Nothing new to learn > Error-prone > Expensive > Slower
  4. 4. > Less human error > Faster! > Decreasing costs > Learning curve
  5. 5. Legacy New/ Improved • SRP • OCP • LSP • ISP • DIP
  6. 6. Unit Tests Integration Tests UI Tests
  7. 7. public test class [Fact] for public methods without parameters [Theory] and [InlineData] for methods with parameters
  8. 8. Build Solution Run Tests in Test Explorer
  9. 9. Verify actual result against expected result Note “var” keyword
  10. 10. Verify that expected view equal to returned view
  11. 11. Temporarily ignore tests with an optional message.
  12. 12. Correct Views? Redirect to URL? Correct Models? HTTP Code? Mock Depen- dencies! Model Populated?
  13. 13. Dependency Injection Inversion of Control
  14. 14. In Startup: Add namespace and dependencies In Application Code: • Constructor Injection • Setter Injection
  15. 15. Mocks Pre-programmed with expected results, doesn’t actually connect to DB, behavior verification. Fakes Working examples, but not production-ready (e..g in-memory database) Stubs Provides canned answers http://martinfowler.com/articles/mocksArentStubs.html
  16. 16. Smoke Tests New Features Edge Cases
  17. 17. Cost of Adding Unit Tests Cost of Not Having Unit Tests Bugs Features Expenses
  18. 18. Views & ViewModels Controllers Service Layer Repository Layer Entity Framework (ORM) SQL Server (database)
  19. 19. 60 to 80% coverage ok?
  20. 20. • Databases • File Systems • Network Resources • Web Requests & Responses
  21. 21. Background > Getting Started > Writing and Running Tests > Dependency Injection & Mocking What’s Next?
  22. 22. Email: shchowd@microsoft.com  Twitter: @shahedC

×