Last active
January 15, 2017 21:01
-
-
Save CopperStarSystems/a47f8f5d2a173d539a11f60785d69fc2 to your computer and use it in GitHub Desktop.
Unit tests after introducing a factory
This file contains 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
using NUnit.Framework; | |
using Moq; | |
[TestFixture] | |
public class ReallyHardToTestTests{ | |
Mock<IPathWrapper> mockPathWrapper; | |
Mock<ISomeOtherDependency> mockSomeOtherDependency; | |
Mock<ISomeModelFactory> mockSomeModelFactory; | |
SomeModel someModel; | |
ReallyHardToTest systemUnderTest; | |
[SetUp] | |
public void SetUp(){ | |
mockPathWrapper = new Mock<IPathWrapper>(MockBehavior.Strict); | |
mockSomeOtherDependency = new Mock<ISomeOtherDependency>(MockBehavior.Strict); | |
mockSomeOtherDependency.Setup(p=>p.DoSomething()); | |
mockSomeModelFactory = new Mock<ISomeModelFactory>(MockBehavior.Strict); | |
someModel = new SomeModel(); | |
mockSomeModelFactory.Setup(p=>p.Create()).Returns(someModel); | |
systemUnderTest = new ReallyHardToTest(mockSomeOtherDependency.Object, mockPathWrapper.Object, mockSomeModelFactory.Object); | |
} | |
[TestCase(@"c:\some\path\filename.txt", "filename.txt")] | |
[TestCase(@"c:\some\really\long\path\otherFile.txt", "otherFile.txt")] | |
public void SetFilename_Always_InvokesPathGetFileName(string input, string expectedFilename){ | |
// Configure our mock expectations | |
mockPathWrapper.Setup(p=>p.GetFileName(input)).Returns(expectedFilename); | |
// Exercise our method under test | |
SystemUnderTest.SetFilename(input); | |
// Verify that our mock's expectations were met | |
mockPathWrapper.VerifyAll(); | |
} | |
[Test] | |
public void Constructor_Always_PerformsExpectedWork(){ | |
// We're testing the constructor, so no Arrange step here. | |
// Likewise with Act - creating our SystemUnderTest is the | |
// Act we're testing here. | |
// Verify that our expectation was met. | |
mockSomeOtherDependency.VerifyAll(); | |
mockSomeModelFactory.VerifyAll(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment