Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
2016-04-30-behavior-driven-development-with-nunit
namespace ConductOfCode
{
public class HelloWorld
{
private readonly IFoo _foo;
private readonly IBar _bar;
public HelloWorld(IFoo foo, IBar bar)
{
_foo = foo;
_bar = bar;
}
public string GetMessage()
{
return _foo.GetFoo() + _bar.GetBar();
}
}
public interface IFoo
{
string GetFoo();
}
public interface IBar
{
string GetBar();
}
}
using Moq;
using NUnit.Framework;
using Should;
namespace ConductOfCode.Tests.Given_HelloWorld
{
public class When_GetMessage
{
private HelloWorld _subject;
private Mock<IFoo> _foo;
private Mock<IBar> _bar;
[SetUp]
public void SetUp()
{
_foo = new Mock<IFoo>();
_bar = new Mock<IBar>();
_subject = new HelloWorld(_foo.Object, _bar.Object);
}
[Test]
public void Should_invoke_IFoo_GetMessage()
{
_subject.GetMessage();
_foo.Verify(x => x.GetFoo());
}
[Test]
public void Should_invoke_IBar_GetMessage()
{
_subject.GetMessage();
_bar.Verify(x => x.GetBar(), Times.Once);
}
[Test]
public void Should_return_a_concatenated_string_with_messages_from_IFoo_and_IBar()
{
_foo.Setup(x => x.GetFoo()).Returns("Hello");
_bar.Setup(x => x.GetBar()).Returns(", World!");
_subject.GetMessage().ShouldEqual("Hello, World!");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.