mocking - Going From State Verification to Behavioral Verification using MOQ -


I am trying to embrace TDD and I am starting to learn about joke. I need some advice on what I should do to test and how to make my classes more behaviors and simple data containers (with a group of gates / sets).

Consider this category.

  public class posts {list & lt; Comment & gt; Comments {get} Private Set;} Public Zero Addcomment (String Message) {Comment.Add (New Comment (Message)); }}  

An example of a state verification test will be

  [test] Public Zero CanAddCommentToPost () {post p = new post (); P.AddComment ("AAAAA"); Assurance Requel (1, comments. Count); }  

I ', of course, is not what I should do for verification of behavior, can someone provide some samples using Moq?

You have to renew your post class, but do not worry.

  Public class posts {Private ILST & lt; Comment & gt; _notes; Public Post (IILIT & lt; comment & gt; CommentContainer) {_comments = commentContainer; } Public Zero AddComment (string message) {_comments.Add (new comment (message)); }}  

This minor redesign will give you the ability to use Muke to confirm your candidate's behavior. I will also show you a better way to name your tests so that it can be clear what they are trying to test.

  [test] Public Zero AddComment_NonNullMessage_IsAddedToCollection {string message = "test message"; // Setup expectations fake & lt; IList & lt; Comment & gt; & Gt; Comment Mock = new fake & lt; IList & lt; Comment & gt; & Gt; (); CommentsMock.Setup (list = & gt; list.Add (new comment (message)); // Make targets, make fake copy posts in the target list = new post (commentsMock.Object); Target.AddComment (message); // Verify our expectations are met by comments Mock.VerifyAll (); }  

And that's all

- Anderson


Comments

Popular posts from this blog

MySql variables and php -

url rewriting - How to implement the returnurl like SO in PHP? -

Which Python client library should I use for CouchdB? -