Using Rhino Mocks for Effective Unit Testing

9,196 views

Published on

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
9,196
On SlideShare
0
From Embeds
0
Number of Embeds
780
Actions
Shares
0
Downloads
77
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Using Rhino Mocks for Effective Unit Testing

  1. 1. Using Rhino Mocks for Effective Unit Testing<br />Mike Clement<br />Utah Code Camp<br />Spring 2011<br />mike@softwareontheside.com<br />@mdclement<br />
  2. 2. Unit Testing Defined<br />“A piece of code that invokes another piece of code and checks the correctness of some assumptions afterward.”<br />
  3. 3. Good Unit Tests<br />Automated and repeatable<br />Easy to implement<br />Remain for future use<br />Anyone should be able to run it<br />Run easily (push of a button)<br />Run quickly<br />
  4. 4. Simple Unit Tests<br />No dependencies<br />
  5. 5. Simple Unit Test Demo<br />Prime Factors<br />
  6. 6. More Complex Unit Tests<br />Simple only gets you so far<br />Need more sophisticated mechanisms<br />Isolation or Mocking Frameworks to the rescue!<br />Does necessitate classes/methods designed for testing<br />
  7. 7. Test Doubles<br />Dummy objects are passed around but never actually used. Usually they are just used to fill parameter lists.<br />Fake objects actually have working implementations, but usually take some shortcut which makes them not suitable for production (an in memory database is a good example).<br />
  8. 8. More Test Doubles (Stubs)<br />Stubs provide canned answers to calls made during the test, usually not responding at all to anything outside what's programmed in for the test. Stubs may also record information about calls, such as an email gateway stub that remembers the messages it 'sent', or maybe only how many messages it 'sent'.<br />In Rhino Mocks:<br />an object that you use in order to pass to the code under test. <br />expectations will never be verified. <br />properties will automatically behave like normal properties, and you can't set expectations on them.<br />IMPORTANT: A stub will never cause a test to fail.<br />MockRepository.GenerateStub<T>()<br />
  9. 9. Rhino Mocks Features<br />Choice of "Arrange, Act, and Assert" or "Explicit record, replay, and verify" models for expectations.<br />Support for Generics, C# Lambda expressions, and other C# 3.0 and .NET 3.5 features<br />Working with strongly typed mocks.<br />Expectations based on:<br />Arguments matching<br />Constraints matching<br />Custom callback to verify the expected arguments using your own code<br />Calling your delegates to decide what to do<br />Setting actions on methods, return specific value, or throw an exception.<br />Much more.<br />
  10. 10. Stubs Demo<br />
  11. 11. Simple and Inline Constraints<br />Specify<br />repository.Stub(x => x.UpdateSession(session))<br />Ignore All<br />IgnoreArguments()<br />Arg<T>.Is.Anything<br />Arg<T>.Is.Null<br />
  12. 12. Arg Constraints<br />Arg<T>.Is<br />Equal(object), NotEqual(object) Comparison using Equals<br />GreaterThan(object), LessThan(object), LessThanOrEqual(object), GreaterThanOrEqual(object) Comparison using >, <, >= and <= operators<br />Same(object), NotSame(object) Reference equality<br />Anything() No constraints<br />Null(), NotNull() Reference is null or not null<br />TypeOf(Type), TypeOf<T>() Argument is of a certain type<br />Matching<T>(Predicate<T>) Argument matches a predicate (.NET 3.5: see Arg<T>.Matches). Example: Arg<string>.Is.Matching(delegate(string s) { return s.Length == 2; }<br />IsIn(object) Enumerable argument includes the specified object<br />Arg<T>.List<br />OneOf(IEnumerable) Argument is in the specified list<br />Equal(IEnumerable) All items in the enumerable argument are compared to the items in the specified list.<br />Count(AbstractConstraint) Constraints to the Count property of the argument.<br />Element(int, AbstractConstraint) Element at the specified index meets the constraint.<br />ContainsAll(IEnumerable) The enumerable argument contains at least the specified items.<br />Arg<T>.Property<br />AllPropertiesMatch(object) All the public properties and public fields are compared to the properties in the specified object. The comparesion is recusive if public properties or fields are complex types.<br />IsNotNull(string propertyName), IsNull(string propertyName) Property of the given name is or is not null<br />Value(string propertyName, object expectedValue) The property of the given name is equal to the expected value.<br />Value(string propertyName, AbstractConstraint constraint) Property of the given Name meets the constraint.<br />Arg.Text<br />StartsWith(string), EndsWith(string), Contains(string) String starts with, ends with or contains the specified text<br />Like(string regex) Property matches to the regular expression<br />Arg<T>.Matches<br />Arg<T>.Matches(Expression) Only in .NET 3.5 you can specify the constraint as a lambda expression, e.g. Arg<int>.Matches(x => x > 3)<br />Arg<T>.Matches(AbstractConstraint) Specify Rhino Mocks Constraints, e.g. Arg<int>.Matches(Is.GreaterThan(0) && Is.LessThan(10))<br />http://www.ayende.com/wiki/Rhino+Mocks+3.5.ashx#ConstraintsReference<br />
  13. 13. More Test Doubles (Mocks)<br />Mocks are objects pre-programmed with expectations which form a specification of the calls they are expected to receive.<br />An object that we can set expectations on, and which will verify that the expected actions have indeed occurred. <br />http://martinfowler.com/articles/mocksArentStubs.html<br />
  14. 14. Mocks<br />Strict vs. Dynamic<br />Use Dynamic Mocks… Strict not recommended<br />MockRepository.GenerateMock<T>()<br />
  15. 15. Mocks Demo<br />
  16. 16. Events!<br />[Test] <br />public void RaisingEvent () <br />{ <br />varmocks = new MockRepository();<br />IView view = mocks.DynamicMock<IView>();<br />Presenter p = new Presenter(view);view.Raise(x => x.Load += null, this, EventArgs.Empty);Assert.IsTrue(p.OnLoadCalled); <br />}<br />
  17. 17. Good Unit Testing Resources!<br />The Art of Unit Testing: With Examples in .Net<br />Pragmatic Unit Testing in C# with NUnit, 2nd Edition<br />http://www.ayende.com/wiki/Rhino+Mocks.ashx<br />
  18. 18. Thank you to our sponsors!<br />Platinum Sponsors<br />Gold Sponsors<br />Silver Sponsors<br />Bronze Sponsors<br />

×