Skip to content

Instantly share code, notes, and snippets.

@hlaueriksson
Last active December 28, 2019 22:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hlaueriksson/79f1dc868fbfc11c65cb8e33d58c0490 to your computer and use it in GitHub Desktop.
Save hlaueriksson/79f1dc868fbfc11c65cb8e33d58c0490 to your computer and use it in GitHub Desktop.
2019-12-29-introducing-lofuunit
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() => _foo.GetFoo() + _bar.GetBar();
}
public interface IFoo
{
string GetFoo();
}
public interface IBar
{
string GetBar();
}
}
using FluentAssertions;
using LoFuUnit.AutoNSubstitute;
using LoFuUnit.NUnit;
using NSubstitute;
using NUnit.Framework;
namespace ConductOfCode
{
public class HelloWorldTests : LoFuTest<HelloWorld>
{
[SetUp]
public void SetUp()
{
Use<IFoo>().GetFoo().Returns("Hello");
Use(Substitute.For<IBar>()).GetBar().Returns(", World!");
}
[LoFu, Test]
public void When_GetMessage()
{
Result = Subject.GetMessage();
void should_invoke_IFoo_GetMessage() =>
The<IFoo>().Received().GetFoo();
void should_invoke_IBar_GetMessage() =>
The<IBar>().Received(1).GetBar();
void should_return_a_concatenated_string_with_messages_from_IFoo_and_IBar() =>
Result.Should().Be("Hello, World!");
}
string Result { get; set; }
}
}
using System;
using System.Collections.Generic;
using FluentAssertions;
using LoFuUnit.NUnit;
using NUnit.Framework;
namespace ConductOfCode
{
public class StackTests
{
[LoFu, Test]
public void When_empty()
{
Subject = new Stack<int>();
void should_have_no_elements() =>
Subject.Should().BeEmpty();
void should_throw_an_exception_when_calling__Pop__() =>
Subject.Invoking(y => y.Pop()).Should().Throw<InvalidOperationException>();
void should_throw_an_exception_when_calling__Peek__() =>
Subject.Invoking(y => y.Peek()).Should().Throw<InvalidOperationException>();
}
[LoFu, Test]
public void When_not_empty()
{
Subject = new Stack<int>(new[] { 1, 2, 3 });
void should_return_the_top_element_when_calling__Peek__() =>
Subject.Peek().Should().Be(3);
void should_not_remove_the_top_element_when_calling__Peek__()
{
var result = Subject.Peek();
Subject.Should().Contain(result);
}
void should_return_the_top_element_when_calling__Pop__() =>
Subject.Pop().Should().Be(3);
void should_remove_the_top_element_when_calling__Pop__()
{
var result = Subject.Pop();
Subject.Should().NotContain(result);
}
}
Stack<int> Subject { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment