在 Rhino Mocks 中处理参数化调用
Handling parameterized calls in Rhino Mocks
我有一个邮件程序 class,它有一个 "SendMessage" 方法,需要一个实现接口 IEMailConfiguration 的对象。我想模拟邮件程序,使其不发送邮件,而只是将给定的邮件配置对象添加到列表中。
这是我目前得到的(不是太多,我知道):
var mailsSent = new List<IEmailConfiguration>();
mailerMock = MockRepository.GenerateStrictMock<Mailer>();
我已经使用 Expect() 和 Do() 调用来模拟无参数调用,这相当简单,但现在我没有看到解决方案:How do i get the argument of the Expect part to be used in做电话吗?
mailerMock.Expect( x=> x.SendMessage(**AnyConfiguration**)).Do(mailsSent.Add(**AnyConfiguration**)
可能您想使用 WhenCalled 方法:
class Program
{
static void Main()
{
List<IEmailConfiguration> performedCalls = new List<IEmailConfiguration>();
// preparing test instance
var mailerMock = MockRepository.GenerateStrictMock<Mailer>();
mailerMock.Expect(x => x.SendMessage(Arg<DummyEmailConfiguration>.Is.Anything))
.WhenCalled(methodInvocation => // that's what you need
{
var config = methodInvocation.Arguments.OfType<IEmailConfiguration>().Single();
performedCalls.Add(config);
});
// testing
for (int i = 0; i < 10; i++)
{
mailerMock.SendMessage(new DummyEmailConfiguration("world" + i + "@mail.com", "hello world" + i));
}
// dumping info
foreach (var call in performedCalls)
{
Console.WriteLine(call);
}
}
}
public interface IEmailConfiguration
{
string To { get; }
string Message { get; }
}
public interface Mailer
{
void SendMessage(IEmailConfiguration config);
}
internal class DummyEmailConfiguration : IEmailConfiguration
{
public DummyEmailConfiguration(string to, string message)
{
To = to;
Message = message;
}
public string To
{
get;
private set;
}
public string Message
{
get;
private set;
}
public override string ToString()
{
return To + ": " + Message;
}
}
我有一个邮件程序 class,它有一个 "SendMessage" 方法,需要一个实现接口 IEMailConfiguration 的对象。我想模拟邮件程序,使其不发送邮件,而只是将给定的邮件配置对象添加到列表中。
这是我目前得到的(不是太多,我知道):
var mailsSent = new List<IEmailConfiguration>();
mailerMock = MockRepository.GenerateStrictMock<Mailer>();
我已经使用 Expect() 和 Do() 调用来模拟无参数调用,这相当简单,但现在我没有看到解决方案:How do i get the argument of the Expect part to be used in做电话吗?
mailerMock.Expect( x=> x.SendMessage(**AnyConfiguration**)).Do(mailsSent.Add(**AnyConfiguration**)
可能您想使用 WhenCalled 方法:
class Program
{
static void Main()
{
List<IEmailConfiguration> performedCalls = new List<IEmailConfiguration>();
// preparing test instance
var mailerMock = MockRepository.GenerateStrictMock<Mailer>();
mailerMock.Expect(x => x.SendMessage(Arg<DummyEmailConfiguration>.Is.Anything))
.WhenCalled(methodInvocation => // that's what you need
{
var config = methodInvocation.Arguments.OfType<IEmailConfiguration>().Single();
performedCalls.Add(config);
});
// testing
for (int i = 0; i < 10; i++)
{
mailerMock.SendMessage(new DummyEmailConfiguration("world" + i + "@mail.com", "hello world" + i));
}
// dumping info
foreach (var call in performedCalls)
{
Console.WriteLine(call);
}
}
}
public interface IEmailConfiguration
{
string To { get; }
string Message { get; }
}
public interface Mailer
{
void SendMessage(IEmailConfiguration config);
}
internal class DummyEmailConfiguration : IEmailConfiguration
{
public DummyEmailConfiguration(string to, string message)
{
To = to;
Message = message;
}
public string To
{
get;
private set;
}
public string Message
{
get;
private set;
}
public override string ToString()
{
return To + ": " + Message;
}
}