Unit Testing with Foq

2,193 views

Published on

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

No Downloads
Views
Total views
2,193
On SlideShare
0
From Embeds
0
Number of Embeds
668
Actions
Shares
0
Downloads
6
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide
  • http://martinfowler.com/bliki/TestingLanguage.html
  • http://nugetmusthaves.com/Tag/mocking
  • Unit Testing with Foq

    1. 1. Unit Testing with Foq@ptrelford on @c4fsharp March 2013Go download on Nuget or foq.codeplex.com
    2. 2. Testing Language
    3. 3. what should a language for writing acceptance tests be?A Language for Testing
    4. 4. Tests operate by example they describe specific scenarios and responsesA Language for Testing
    5. 5. I wonder if a different kind of programming language is required. - Martin Fowler 2003A Language for Testing
    6. 6. UNIT TESTING WITH F#F# as a Testing Language
    7. 7. F# NUnit C# NUnitmodule MathTest = using NUnit.Framework;open NUnit.Framework [TestFixture] public class MathTest {let [<Test>] ``2 + 2 should equal 4``() = [Test] Assert.AreEqual(2 + 2, 4) public void TwoPlusTwoShouldEqualFour() { Assert.AreEqual(2 + 2, 4); } }NUnit
    8. 8. let [<Test>] ``2 + 2 should equal 4``() = 2 + 2 |> should equal 4FsUnit
    9. 9. let [<Test>] ``2 + 2 should equal 4``() = test <@ 2 + 2 = 4 @>Unquote
    10. 10. .NET MOCKINGF# as a Testing Language
    11. 11. Mocking libraries
    12. 12. unittesting
    13. 13. var mock = new Mock<ILoveThisFramework>();// WOW! No record/replay weirdness?! :)mock.Setup(framework => framework.DownloadExists("2.0.0.0")) .Returns(true) .AtMostOnce();// Hand mock.Object as a collaborator and exercise it,// like calling methods on it...ILoveThisFramework lovable = mock.Object;bool download = lovable.DownloadExists("2.0.0.0");// Verify that the given method was indeed called with the expectedvaluemock.Verify(framework => framework.DownloadExists("2.0.0.0"));Moq
    14. 14. // Creating a fake object is just dead easy!// No mocks, no stubs, everythings a fake!var lollipop = A.Fake<ICandy>();var shop = A.Fake<ICandyShop>();// To set up a call to return a value is also simple:A.CallTo(() => shop.GetTopSellingCandy()).Returns(lollipop);// Use your fake as you would an actual instance of the faked type.var developer = new SweetTooth();developer.BuyTastiestCandy(shop);// Asserting uses the exact same syntax as when configuring calls,// no need to teach yourself another syntax.A.CallTo(() => shop.BuyCandy(lollipop)).MustHaveHappened();FakeItEasy
    15. 15. Mock object Object ExpressionMock<IShopDataAccess>() { new IShopDataAccess with .Setup(fun data -> member __.GetProductPrice(productId) = <@ data.GetProductPrice(any()) @>) match productId with .Calls<int>(function | 1234 -> 45M | 1234 -> 45M | 2345 -> 15M | 2345 -> 15M | _ -> failwith "Unexpected" | productID -> failwith "Unexpected" member __.Save(_,_) = ) failwith "Not implemented" .Create() }F# Object Expressions
    16. 16. FOQ MOCKINGF# as a Testing Language
    17. 17. WTF
    18. 18. // Arrangelet xs = Mock<IList<char>>.With(fun xs -> <@ xs.Count --> 2 xs.Item(0) --> 0 xs.Item(1) --> 1 xs.Contains(any()) --> true xs.RemoveAt(2) ==> System.ArgumentOutOfRangeException() @> )// AssertAssert.AreEqual(2, xs.Count)Assert.AreEqual(0, xs.Item(0))Assert.AreEqual(1, xs.Item(1))Assert.IsTrue(xs.Contains(0))Assert.Throws<System.ArgumentOutOfRangeException>(fun () -> xs.RemoveAt(2))Foq: IList<char>
    19. 19. var order = new Mock<IOrder>() .SetupProperties(new { Price = 99.99M, Quantity = 10, Side = Side.Bid, TimeInForce = TimeInForce.GoodTillCancel }) .Create();Assert.AreEqual(99.99M, order.Price);Assert.AreEqual(10, order.Quantity);Assert.AreEqual(Side.Bid, order.Side);Assert.AreEqual(TimeInForce.GoodTillCancel,order.TimeInForce);Foq: Anonymous Type
    20. 20. let [<Test>] ``order sends mail if unfilled`` () = // setup data let order = Order("TALISKER", 51) let mailer = mock() order.SetMailer(mailer) // exercise order.Fill(mock()) // verify verify <@ mailer.Send(any()) @> onceFoq: Type Inference
    21. 21. let [<Test>] ``verify sequence of calls`` () = // Arrange let xs = Mock.Of<IList<int>>() // Act xs.Clear() xs.Add(1) // Assert Mock.VerifySequence <@ xs.Clear() xs.Add(any()) @>Foq Sequences
    22. 22. FOQ DEPLOYMENTF# as a Testing language
    23. 23. Nuget Dowload CodePlex DownloadDeployment
    24. 24. FOQ APIF# as a testing language
    25. 25. Setup a mock method in C# with a lambda expression:new Mock<IList<int>>() .Setup(x => x.Contains(It.IsAny<int>())).Returns(true) .Create();Setup a mock method in F# with a Code Quotation:Mock<System.Collections.IList>() .Setup(fun x -> <@ x.Contains(any()) @>).Returns(true) .Create()LINQ orQuotations
    26. 26. Fluent Interface orFunctions
    27. 27. FOQ IMPLEMENTATIONF# as a testing language
    28. 28. Moq FakeItEasyTotal 16454 Total 11550{ or } 2481 { or } 2948Blank 1933 Blank 1522Null checks 163 Null checks 92Comments 7426 Comments 2566Useful lines 4451 Useful lines 4422LOC: Moq vs FakeItEasy
    29. 29. Fock v0.1 Fock v0.2127 Lines 200 Lines• Interfaces • Interfaces• Methods • Abstract Classes• Properties • Methods • Properties • Raise ExceptionsFock (aka Foq)
    30. 30. Foq.fs Foq.fs + Foq.Linq.fsTotal 666 Total 933LOC: Foq 0.8.1
    31. 31. QUESTIONSF# as a Testing Language
    32. 32. What The Foq?

    ×