English 中文(简体)
单位测试/如何采用非磁性方法,称为C#/Rhino Mocks/NUnit
原标题:Unit testing/How to assert a non-mocked method was called/C#/Rhino Mocks/NUnit

我有一个邮局,允许我发送电子邮件,以落实以下内容。

public interface IMailService
  {
    bool SendRegisteringEmail(RegisterModel registerModel);
    bool SendMail(MailMessage mailMessage);
    MailMessage CreateRegisteringMailMessage(RegisterModel registerModel);

    IAppSettingsRepository AppSettingsRepository { get; set; }

    ISmtpClient SmtpClient { get; set; }
  }

SendRegisteringEmail功能应当叫创建RegisteringmailMessage,然后让Sendmail功能返回,SendRegisteringEmail应当返回Sendmail的Bolean。

I m using NUnit and Rhino Mocks to do my test, I m new to testing (1week) and I m practicing TDD (At least I try). My problem is I don t know how to assert that CreateRegisteringMailMessage was called when I call SendRegisteringEmail, because MailService isn t a Mocked Object. Here is my test:

[Test]
public void SendRegisteringEmail_ShouldCallCreateRegisteringMailMessage()
{
  //Arrange
  //MailMessage mailMessage = new MailMessage();
  IMailService mailService = new MailService { AppSettingsRepository = CreateAppSettingsMockReposotory() };
  //ISmtpClient smtpClientStub = MockRepository.GenerateStub<ISmtpClient>();
  //mailService.SmtpClient = smtpClientStub;
  //smtpClientStub.Stub(f => f.Send(mailMessage)).Return(true);


  //Act
  mailService.SendRegisteringEmail(ValidRegisterModel);

  //Assert
  mailService.AssertWasCalled(f => f.CreateRegisteringMailMessage(ValidRegisterModel));
}

I get the following error when I launch my test: FtpWeb.Tests.MailServiceTests.SendRegisteringEmail_ShouldCallCreateRegisteringMailMessage: System.InvalidOperationException : The object FtpWeb.Models.MailService is not a mocked object.

I understand why I m getting this error but now how to test my call. Since it s the same object I can t mock it to test it. If anybody can give me some lead to resolve this.

感谢。

最佳回答

<代码>CreateRegisteringmailMessage和Sendmail/的抽象程度似乎低于。 修正 电子邮件:。 您可考虑设立一个包含<条码>的更高级别的班级,该班级将使用电子邮件服务,您可以照常说。

如果第一种解决办法不是一种选择,那么你就应当作为一个整体处理>SendRegisteringEmail,那么,你就必须在一次测试中测试>CreateRegisteringmailMessageSendmail的副作用(一种密码的缩略语,因此我建议从间接角度提取另一个层次)——见Jons的答复。

问题回答

页: 1 http://www.un.org。 您仅应查询SendRegistering Email<>。 如何执行? 假定这将对另一个服务产生一定的影响,这样就可以进行测试。

这是一种奇迹接口设计,它坚持认为,采用一种方法将需要在接口中采用另一种方法——通常你会把这两种方法称为一种私人通用方法。

这常常被指在测试的类别中进行的一种测量:你必须找到一种方法来说明是否使用了某种方法。 http://www.amazon.co.uk/Working-Effectively-Legacy-Robert-Martin/dp/0131177052/ref=sr_1_1?s=books&ie=UTF8&qid=12811407&sr=1-1”rel=“nofollow noreferer”与《遗产法典》有效合作,人人都应当有(争端者:我与作者没有关系,我认为作者有好书。

。 这是否影响到打电话时通过的<代码>RegisterModel的内容?





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...