200 likes | 357 Views
Taking your tests to the next level with Mocks. Where are we going today?. Learning the P’s & Q’s Walk through the evolution of testing Learn to break dependencies with Mocks Learn how testing changes your code design. Learning the Terms. Mocks vs. Stubs AAA vs. Record/Replay semantics
E N D
Where are we going today? Learning the P’s & Q’s Walk through the evolution of testing Learn to break dependencies with Mocks Learn how testing changes your code design
Learning the Terms • Mocks vs. Stubs • AAA vs. Record/Replay semantics • Type of Mocks • Strict Mocks – Strict replay semantics • Dynamic Mocks – Loose replay semantics • Partial Mocks – Mock only requested methods • With MockRepository Instance vs Static MockRepository • AutoMocking Containers
Why Should I Mock • Test in Isolation • No fear of external resources breaking • Web Services • Database’s • Test edge conditions • Easily force failures • Simulation of Objects • Reduces overhead
Unit Test vs. Integration Test • What is a ‘Unit Test’ • The smallest testable part of an application. The code should be tested in isolation by removing dependencies in order to have reliable tests. • What is a ‘Integration Test’ • End to end testing either within a module or application. Code for the tests are coupled and tested as a whole unit.
Dependency Barrier Breaking Dependencies with Mocks Tests without Mocks Tests with Mocks
Then came…. UserLogin userLogin = ObjectFactory.GetInstance<UserLogin>(); // 1 - Need to find a username that is NOT already in use by another user // 2 - Need to find a password that is complexe enough User createdUser = userLogin.CreateUser("SomeUnusedUsername", "S0meP@ssw0rd", "FirstName", "LastName"); // Need to do our asserts on evertying …
Now we have…. varmocker = newMockRepository(); varmockRepository = mocker.PartialMock<ValidationRepository>( new ConfigurationReader(), null ); mockRepository.Expect( x => x.ExecuteDataSet( command, new SqlParameter[ 0 ] ) ).IgnoreArguments().Return( dataSet).Repeat.Once(); varresult = mockRepository.HasViolationForVisitDateTimeChanged( 12, serverDate, serverTime ); Assert.That( result, Is.Not.Null ); Assert.That( result.Success, Is.EqualTo( true ) );
Example 1 • We would like to be able to create a new user for our system. • Requirements • All valid data must be provided • Password must meet complexity standards • Username must not already exist
Lets Code • Review a test with NO MOCKS • Review pain points • Review same test WITH MOCKS • Review pain points
Example 2 • If the user forgets their password we would like to email it to them • Requirements • Username must exist • User must answer challenge question correctly • Send email to user with valid information
Lets Code • Create a test where we want our email service to fail • Review pain points
Example 3 Demo partial mocking abilities Lets code….
Lets Code • Create a test where we want our email service to fail • Review pain points
Example 4 Redoing Example 2 but with an Automocking Container Lets code….
Thoughts on Design for Testability • Interfaces driven development • Makes us use contracts • Single Responsibility Principle • Forces us to think about what it is we want each ‘thing’ to do. • Separation of Concerns • Allows us to de-couple our applications. • No more ‘be all, end all’ objects • Inversion of Control/Dependency Injection • Flipping our design our its head • Allows us to simply mock dependencies as well
Mocking Tips/Gotcha’s Can only mock ‘public’ classes or interfaces Can only mock interfaces, delegates and virtual methods of classes! Cannot mock sealed classes.