Part of my project interacts with iTunes using COM. The purpose of this test is to verify that my object asks the iTunes API to export all album art for a collection of tracks to a file.
I successfully wrote a test that can prove that my code does this, however, to do this I had to cut out a piece of the iTunes implementation, while this can be expected in the unit test the ratio of the installation code of the stub against the code that does the actual testing
My questions:
- Is the fact that there is still installation code for the stub, and then valid code pointing to another major problem in my code>
- There is a lot of installation code, and I do not believe that repeating it for a test is a good idea. What is the best way to reorganize this code so that this setup code is separate from it, but is available for other tests that should use stubs.
This seam is similar to a question that may have been asked before, so I’ll approach in advance if I created a duplicate
For reference, here is a complete unit test that bothers me
[Fact]
public void Add_AddTrackCollection_AsksiTunesToExportArtForEachTrackInCollectionToAFile()
{
var trackCollection = MockRepository.GenerateStub<IITTrackCollection>(null);
var track = MockRepository.GenerateStub<IITTrack>(null);
var artworkCollection = MockRepository.GenerateStub<IITArtworkCollection>(null);
var artwork = MockRepository.GenerateMock<IITArtwork>(null);
var artworkCache = new ArtworkCache();
trackCollection.Stub<IITTrackCollection, int>(collection => {return collection.Count; }).Return(5);
trackCollection.Stub<IITTrackCollection, IITTrack>(collection => { return trackCollection[0]; }).IgnoreArguments().Return(track);
track.Stub<IITTrack, IITArtworkCollection>(stub => { return stub.Artwork; }).Return(artworkCollection);
artworkCollection.Stub<IITArtworkCollection, int>(collection => { return collection.Count; }).Return(1);
artworkCollection.Stub<IITArtworkCollection, IITArtwork>(collection => { return artworkCollection[0]; }).IgnoreArguments().Return(artwork);
artwork.Expect<IITArtwork>(stub => { stub.SaveArtworkToFile(null); }).IgnoreArguments().Repeat.Times(trackCollection.Count-1);
artwork.Replay();
artworkCache.Add(trackCollection);
artwork.VerifyAllExpectations();
source
share