Reputation: 27360
I have the following code, which passes interface into a function which expects a dictionary list of protocol handlers:
var _protocolHandlers = new Dictionary<EmailAccountType, IEmailTransportHandler>
{
{EmailAccountType.Pop3, new Mock<IEmailTransportHandler>().Object},
{EmailAccountType.IMAP, new Mock<IEmailTransportHandler>().Object}
};
Strange thing is though, that the following code doesn't give me the set-up methods on the mocked object:
_protocolHandlers[0].<where are set-up methods??>
It seems to follow the convention with normal interfaces passed into the constructor of a service, as these take interfaces, but they are injected using .Object().
Does anyone have a clue what is going on here?
Upvotes: 0
Views: 1970
Reputation: 35793
The Setup methods are on the Mock 'container', not the actual mocked object which is what has been passed in.
If you create your mocks before, you'll be able to access the setup then pass in the object:
[TestFixture]
public class MyTest
{
Dictionary<EmailAccountType, IEmailTransportHandler> _protocolHandlers;
Mock<IEmailTransportHandler> _mockEmailTransportHander = new Mock<IEmailTransportHandler>();
[SetUp]
public void Init()
{
_protocolHandlers = new Dictionary<EmailAccountType, IEmailTransportHandler>
{
{EmailAccountType.Pop3, _mockEmailTransportHander.Object},
{EmailAccountType.IMAP, _mockEmailTransportHander.Object}
};
}
[Test]
public void Test1()
{
_mockEmailTransportHander.Setup(m => m.Test()).Returns(false);
// Rest of test
}
[Test]
public void Test2()
{
_mockEmailTransportHander.Setup(m => m.Test()).Returns(true);
// Rest of test
}
}
Upvotes: 5