Advertisement
Guest User

Untitled

a guest
Nov 24th, 2014
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.25 KB | None | 0 0
  1.  
  2.  
  3.     // mocking simple interface with a predefined return value
  4.    var myInterface = Mock.Of<IMyInterface>(x => x.Foo() == "bar");
  5.    Console.WriteLine(myInterface.Foo());
  6.    > "bar"
  7.  
  8.    // You can also only allow a return value if you pass in a specific object..
  9.    var myObject = new object();
  10.    var myInterface = Mock.Of<IMyInterface>(x => x.Foo(myObject) == "bar");
  11.    Console.WriteLine(myInterface.Foo(null));
  12.    > null
  13.    Console.WriteLine(myInterface.Foo(myObject));
  14.    > "bar"
  15.  
  16.    // Matchers are avaliable too
  17.    var myInterface = Mock.Of<IMyInterface>(x => x.Foo(It.IsAny<IBaz>(y => y.Baz == "Qux") == "Bar"));
  18.    var baz = new Baz { Baz = "Qux" };
  19.    var baz2 = new Baz { Baz = "Quas" };
  20.    Console.WriteLine(myInterface.Foo(baz));
  21.    > "Bar"
  22.    Console.WriteLine(myInterface.Foo(baz2));
  23.    > null
  24.  
  25.    // You can also interact with passed parameters by using a slightly more complex syntax
  26.    var mockMyInterface = new Mock<IMyInterface>();
  27.    mockMyInterface.Setup(myInterface => myInterface.Foo(It.IsAny<int>())).Returns<int>(i => i * 2);
  28.    var myInterface = mockMyInterface.Object; // Mock object is exposed through Object property
  29.    Console.WriteLine(myInterface.Foo(42));
  30.    > 84
  31.    Console.WriteLine(myInterface.Foo(3));
  32.    > 6
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement