Created
October 19, 2012 23:16
-
-
Save lanceharper/3921245 to your computer and use it in GitHub Desktop.
Test the widget
This file contains 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 Widget | |
{ | |
public interface IDeduceable | |
{ | |
bool Deduce(); | |
} | |
public class FooA | |
{ | |
public FooA(FooB fb) { } | |
public bool Deduce() | |
{ | |
return false; // who knows, I'm not calling this anyway. | |
} | |
} | |
public class FooB | |
{ | |
public FooB(FooC fc, FooD fd) | |
{ | |
} | |
} | |
public class FooC | |
{ | |
public FooC(FooD fd) | |
{ | |
} | |
} | |
public class FooD | |
{ | |
} | |
public class FooAAdapter : IDeduceable // Wire this up in an IoC Container. | |
{ | |
public FooA fooA = new FooA(new FooB(new FooC(new FooD()), new FooD())); | |
public bool Deduce() | |
{ | |
return fooA.Deduce(); | |
} | |
} | |
public class Widget | |
{ | |
private IDeduceable fooA; | |
public Widget(IDeduceable fooA) | |
{ | |
this.fooA = fooA; | |
} | |
public void Execute() | |
{ | |
if (this.fooA.Deduce()) | |
this.DoSomethingAwesome(); | |
else | |
this.DoSomethingLessAwesome(); | |
} | |
private void DoSomethingAwesome() | |
{ | |
Console.WriteLine("Is this awesome enough?"); | |
} | |
private void DoSomethingLessAwesome() | |
{ | |
Console.WriteLine("This is slightly less awesome."); | |
} | |
} | |
} |
This file contains 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 Widget.Test | |
{ | |
[TestClass] | |
public class WidgetTest | |
{ | |
public class FakeDeducer : IDeduceable // Manual faking used in lieu of a mocking framework due to simplicity. | |
{ | |
public bool DetermineDeduction { get; set; } | |
public bool Deduce() | |
{ | |
return DetermineDeduction; | |
} | |
} | |
[TestMethod] | |
public void Widget_should_be_awesome_when_foo_is_deduced() | |
{ | |
var widget = new Widget(new FakeDeducer { DetermineDeduction = true}); | |
using (var stringWriter = new StringWriter()) | |
{ | |
Console.SetOut(stringWriter); | |
widget.Execute(); | |
var expected = | |
string.Format("Is this awesome enough?{0}", Environment.NewLine); | |
var actual = stringWriter.ToString(); | |
Assert.AreEqual<string>(expected, actual); | |
} | |
} | |
[TestMethod] | |
public void Widget_should_be_less_awesome_when_foo_is_not_deduced() | |
{ | |
var widget = new Widget(new FakeDeducer { DetermineDeduction = false }); | |
using (var stringWriter = new StringWriter()) | |
{ | |
Console.SetOut(stringWriter); | |
widget.Execute(); | |
var expected = | |
string.Format("This is slightly less awesome.{0}", Environment.NewLine); | |
var actual = stringWriter.ToString(); | |
Assert.AreEqual<string>(expected, actual); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment