Skip to content

Instantly share code, notes, and snippets.

@rofr
Created April 1, 2014 15:09
Show Gist options
  • Select an option

  • Save rofr/9916118 to your computer and use it in GitHub Desktop.

Select an option

Save rofr/9916118 to your computer and use it in GitHub Desktop.
First attempt, well almost, at using FakeItEasy
[Test]
public void Timestamp_is_transfered_to_journalentry()
{
JournalEntry entry = null;
var fake = A.Fake<IJournalWriter>();
A.CallTo(() => fake.Write(A<JournalEntry>._))
.Invokes((JournalEntry je) =>
{
entry = je;
});
var command = new SetTimeCommand();
command.Timestamp = DateTime.Now;
var target = new JournalAppender(0, fake);
target.Append(command);
Assert.IsNotNull(entry);
Assert.AreEqual(command.Timestamp, entry.Created);
}
@blairconrad
Copy link
Copy Markdown

Looks good. An alternative may be

Test]
public void Timestamp_is_transfered_to_journalentry()
{
    var fake = A.Fake<IJournalWriter>();

    var command = new SetTimeCommand();
    command.Timestamp = DateTime.Now;
    var target = new JournalAppender(0, fake);
    target.Append(command);

    A.CallTo(() => fake.Write(A<JournalEntry>.That.Matches(je => je != null &&
                                                                 je.Created == command.Timestamp)))
                       .MustHaveHappened();
}

Of course, I am not a compiler, so I may be slightly off.
It at least partly comes down to whether you prefer NUnit's messages or FakeItEasy's.
In this case, though, I think the MustHaveHappened is more intention-revealing than the Invokes and the extra entry variable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment