douhulao7642
2013-12-24 11:54
浏览 43
已采纳

PHPUnit中的模拟对象有什么意义?

I'm developing a intern app which send some emails in a view methods. Now I'm rebuilding this app TDD-style but I'm stuck at some point. I searched the web for how to test emails with PHPUnit and the solution was, use mock objects. I read some articles and tutorials about it and built a test with a mock object.

The test passed but I don't understand why should use a mock object if you already know the result. I mean, if you already know the result you don't see the failure on your real method? So is this not the way to test methods which use the email function or am I just doing it wrong? Probably the second one haha.

Thanks in advance,

Sjors

图片转代码服务由CSDN问答提供 功能建议

我正在开发一个实习应用程序,它在视图方法中发送一些电子邮件。 现在我正在重建这个应用程序TDD风格,但我在某个时候陷入困境。 我在网上搜索了如何使用PHPUnit测试电子邮件,解决方案是,使用模拟对象。 我阅读了一些关于它的文章和教程,并使用模拟对象构建了一个测试。

测试通过,但我不明白为什么要使用模拟对象,如果你已经知道结果。 我的意思是,如果你已经知道结果,你没有看到真正的方法失败? 那么这不是测试使用电子邮件功能的方法的方法,还是我只是做错了? 可能是第二个哈哈。

提前致谢,

Sjors

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dou72260 2013-12-24 12:21
    已采纳

    When you are unit testing you should be trying to test your method under test (unit of work under test) in isolation from the rest of your system. As soon as you are forced to cross a system boundary, your unit test becomes an integration test. Integration tests tend to be more fragile, harder write and harder to maintain. By isolating the mailing component from the method that creates the mail you no longer need to rely on whether the mailing system is up and running when you unit test runs. You are free to test the logic in the method under test without having to depend on a concrete implementation.

    There are two categories of mock tests, interaction tests and state based tests. I'd suggest looking up the difference between the two on google.

    I am unfortunately not familiar with the syntax of php to I can only show you an example of what you might be testing in your case using C# syntax, but I am sure you will be able to follow the code.

    public interface Mailer
    {
      void Send(string to, string from, string subject, string body)
    }
    

    The above interface would be one that matches the signature of your mailing component that actually sends the mail (it knows about smtp etc).

    public class MailSender
    {
      private IMailer _mailer;
      public MailSender(IMailer mailer)
      {
        _mailer = mailer;
      }
    
      public void GenerateMail(string to, string cc, string from, string subject, string body)
      {
        if (IsValidEmailAddress(to) == false) throw new InvalidEmailAddressException("To"); 
        if (IsEmptySubject(from) == false) throw new EmptySubjectException();    
        _mailer.Send(to, from, subject, body);
      }
    
      private bool IsValidEmailAddress(string emailAddress)
      {
        // some code/regex to check that the email address is valid
      }
    
      private boold IsEmptySubject(string subject)
      {
        // code to check if the subject is empty
      }
    }
    

    Now we have logic in the GenerateMail method. You can write unit tests to check if you get exceptions for invalid email addresses and an empty subject and you can also write a unit test to ensure that if you have valid email addresses and a subject that is not empty the send method is called on the mocked mail sender with the correct information in each of the parameters. This type of unit test would an interaction test as the mock is just verifying that the method was called).

    Hope this explanation helps.

    点赞 打赏 评论
  • douchui4815 2013-12-24 12:09

    The reason to use mock objects is isolation. If you write unit tests you want them to be as isolated from other systems or code as possible. Hence the name unit tests. Using mocks allows you to isolate your tests from other systems (email in your case). There is a talk by Misko Hevery http://www.youtube.com/watch?v=acjvKJiOvXw that explains this aspect better than I ever could.

    点赞 打赏 评论

相关推荐 更多相似问题