views:

127

answers:

1

The second assertion never executes in the unit test below:

namespace Foo {
    public class MyClass {
    }
}

namespace Bar {
    public class MyClass {
    }
}

namespace Quux {
    public interface IRepo {
     object Get<T>() where T : new();
    }
}

namespace Tests {
    [TestFixture]
    public class MyTests {
     private Mock<Quux.IRepo> repo = new Mock<Quux.IRepo>();

     [SetUp]
     public void Setup() {
      repo.Setup(r => r.Get<Foo.MyClass>()).Returns(new Object());
      repo.Setup(r => r.Get<Bar.MyClass>()).Returns(new Object());
     }

     [Test]
     public void Test() {
      Assert.IsNotNull(repo.Object.Get<Foo.MyClass>());
      Assert.IsNotNull(repo.Object.Get<Bar.MyClass>()); // Never reached.
     }
    }
}

Looks like setting up Get<Bar.MyClass>() overwrites the setup for Get<Foo.MyClass>(). Does this mean Moq ignores the namespaces Foo and Bar when setting up Get<T>()? Is there a way around this without renaming the MyClass classes and without implementing IRepo? I'm using Moq 3.1.416.3.

Update: Looks like this is fixed for next release.

+1  A: 

Moq does not ignore namespaces. This looks like a bug. The right place to report it would be at http://code.google.com/p/moq/issues/entry

kzu
Thanks. I'll report it when I have a chance.
Jonathon Watney
Issue reported: http://code.google.com/p/moq/issues/detail?id=187
Jonathon Watney