Created
May 14, 2014 15:04
-
-
Save MorganPersson/f306383dfce4f865dc67 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace Bad | |
open System; | |
module BadDependency = | |
let BadFunction x = | |
Random().Next(0, x) | |
module TestMe = | |
let FuncToTest y = | |
(y + BadDependency.BadFunction 100) % 2 = 0 | |
module Test = | |
let Test () = | |
// Can't replace call to BadFunction | |
let result = TestMe.FuncToTest 42 | |
0 | |
/////////////////////////////////// | |
// Refactor to this | |
/////////////////////////////////// | |
namespace Better | |
open System; | |
module BadDependency = | |
let BadFunction x = | |
System.Random().Next(0, x) | |
module TestMe = | |
let TestableFuncToTest f y = | |
(y + f 100) % 2 = 0 | |
[<Obsolete("Use TestableFuncToTest")>] | |
let FuncToTest y = TestableFuncToTest BadDependency.BadFunction | |
module Test = | |
let Test () = | |
let mock x = 2 | |
let result = TestMe.TestableFuncToTest mock 42 | |
// Assert is now possible | |
0 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment