Why this code doesn't work?
var channelsList = new List<IChannel>
{
Mock.Of<IChannel>(m => m == new ChannelOne()),
Mock.Of<IChannel>(m => m == new ChannelTwo()),
};
Why this code doesn't work?
var channelsList = new List<IChannel>
{
Mock.Of<IChannel>(m => m == new ChannelOne()),
Mock.Of<IChannel>(m => m == new ChannelTwo()),
};
Assuming that IChannel
is defined as:
public interface IChannel
{
int DoWork();
int DoOtherWork();
}
Then you could define different behavior using Moq.Linq
like this:
var channelsList = new List<IChannel>
{
Mock.Of<IChannel>(m => m.DoWork() == 1 && m.DoOtherWork() == 1),
Mock.Of<IChannel>(m => m.DoWork() == 2)
};
Assert.Equal(1, channelsList.First().DoWork());
Assert.Equal(2, channelsList.Last().DoWork());
There is however limitation that you cannot setup Throws
for example...
LINQ to Mocks is great for quickly stubbing out dependencies that typically don't need further verification. If you do need to verify later some invocation on those mocks, you can easily retrieve them with
Mock.Get(instance)
.
note:emphasis mine