I am new to both MOQ and TDD / Unit Testing. I find myself repeating a lot of the same code in my "Arrange" section of each test method and it seems there really ought to be a better way.
First the code that get's oft repeated:
#region Arrange
#region create my Mock Objects
var serviceMock = new Mock<IOrganizationService>();
var factoryMock = new Mock<IOrganizationServiceFactory>();
var tracingServiceMock = new Mock<ITracingService>();
var notificationServiceMock = new Mock<IServiceEndpointNotificationService>();
var pluginContextMock = new Mock<IPluginExecutionContext>();
var serviceProviderMock = new Mock<IServiceProvider>();
#endregion
#region Authentication Stuff
var organizationUri = "http://.../XRMServices/2011/Organization.svc";
var orgServiceManagement = ServiceConfigurationFactory.CreateManagement<IOrganizationService>(new Uri(organizationUri));
var credentials = new AuthenticationCredentials();
var username = "USER_ID";
var password = "USER_PWD";
var domain = "MyDomain";
credentials.ClientCredentials. Windows.ClientCredential = new NetworkCredential(username, password, domain);
IOrganizationService service = new OrganizationServiceProxy(orgServiceManagement, credentials.ClientCredentials);
#endregion
#endregion
I considered making custom snippets, constants, etc. but that would not really save the repetitious code just save me some time typing.
Next I thought about scoping everything above at the class level...but some warning bells went off...If the class instantiates once for all test methods then could there be the possibility of variable pollution? Given in the small sample here it really wouldn't happen but I'm trying to think ahead / develop good practices and habits.
What about creating a "Mock & Authentication" object that had all these defined in that object. Then I could have several of these objects with different credentials and my test methods could reference / use just the one variation needed?
So what would be your advice / recommendations for applying some DRY principles to my test cases.
First, your setup is really large. If this is an unit test, this calls for refactoring. Are you sure your components under test do one thing and one thing only? Judging by the names of dependencies, I would assume no.
Next I thought about scoping everything above at the class level...but some warning bells went off...If the class instantiates once for all test methods then could there be the possibility of variable pollution?
No, most test runners create test class instance per test run and unit test frameworks usually allow sort of before test setup, a piece of code (usually method or constructor) that does just this.
In NUnit you got [SetUp]
attribute (MS Test uses [TestInitialize]
while xUnit uses class constructor):
[SetUp]
public void InitializeDependencies()
{
serviceMock = new Mock<IOrganizationService>();
// ...
}
This is how you usually solve this kind of problems.
What about creating a "Mock & Authentication" object that had all these defined in that object.
While this might be a good idea for authentication part, it certainly is not as sound for mocks. The fact you even consider it rings the refactoring bells once again.