Skip to content

Instantly share code, notes, and snippets.

@CopperStarSystems
Last active January 16, 2017 17:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CopperStarSystems/2a59d7b795ff87637005d50ce5946d49 to your computer and use it in GitHub Desktop.
Save CopperStarSystems/2a59d7b795ff87637005d50ce5946d49 to your computer and use it in GitHub Desktop.
Refactoring our test class to use MockRepository
using NUnit.Framework;
using Moq;
[TestFixture]
public class ReallyHardToTestTests{
MockRepository mockRepository = new MockRepository(MockBehavior.Strict);
Mock<IPathWrapper> mockPathWrapper;
Mock<ISomeOtherDependency> mockSomeOtherDependency;
Mock<ISomeModelFactory> mockSomeModelFactory;
SomeModel someModel;
ReallyHardToTest systemUnderTest;
[SetUp]
public void SetUp(){
mockPathWrapper = mockRepository.Create<IPathWrapper>();
mockSomeOtherDependency = mockRepository.Create<ISomeOtherDependency>();
mockSomeOtherDependency.Setup(p=>p.DoSomething());
mockSomeModelFactory = mockRepository.Create<ISomeModelFactory>();
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
mockRepository.VerifyAll();
Assert.That(someModel.Filename, Is.EqualTo(expectedFilename));
}
[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.
mockRepository.VerifyAll();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment