Created
December 22, 2010 15:01
-
-
Save carlhoerberg/751617 to your computer and use it in GitHub Desktop.
Test IModel in RabbitMQ.Client
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[TestFixture] | |
public class RabbitMQTest | |
{ | |
[Test] | |
public void CanMockMQ() | |
{ | |
// Arrange | |
var channelMock = new Mock<IModel>(); | |
channelMock | |
.Setup(m => m.BasicConsume(It.IsAny<string>(), It.IsAny<bool>(), It.IsAny<IDictionary>(), It.IsAny<IBasicConsumer>())) | |
.Callback<string, bool, object, IBasicConsumer>((queue, noAck, props, consumer) => consumer.HandleBasicDeliver("", 1, false, "exchange", "route", null, new byte[0])); | |
var server = new MQUser(channelMock.Object); | |
// Act | |
server.Abort(); | |
server.DoStuff(); | |
// Assert | |
channelMock.Verify(m => m.BasicAck(1, false)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MQUser | |
{ | |
readonly IModel channel; | |
public MQUser(IModel channel) | |
{ | |
this.channel = channel; | |
} | |
public void DoStuff() | |
{ | |
var queueName = channel.QueueDeclare(); | |
var subscription = new Subscription(channel, queueName); | |
do | |
{ | |
var msg = subscription.Next(); | |
// Do stuff with the message | |
subscription.Ack(msg); | |
} while (!aborted); | |
} | |
bool aborted; | |
public void Abort() | |
{ | |
aborted = true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment