Skip to content

Instantly share code, notes, and snippets.

@panesofglass
Created July 21, 2009 18:19
Show Gist options
  • Save panesofglass/151500 to your computer and use it in GitHub Desktop.
Save panesofglass/151500 to your computer and use it in GitHub Desktop.
Test format examples with xUnit.NET and NBehave
namespace Cloud.ATS.Domain.Tests
{
using Entities;
using NBehave.Narrator.Framework;
using Xunit;
using Xunit.AssertExtensions;
using Xunit.Specifications;
public class JobRepositoryTests
{
private readonly IRepository<Job> _jobRepository;
/// <summary>
/// Set up for each test.
/// </summary>
/// <remarks>
/// xUnit.NET doesn't require attributes for the setup method.
/// Implement dispose to tear down for each test.
/// Implement IUseFixture&gt;T&lt; to setup and tear down for the test suite.
/// </remarks>
public JobRepositoryTests()
{
_jobRepository = new TestRepository<Job>();
}
[Fact]
public void ShouldAddANewJob()
{
Job job = new Job();
_jobRepository.Add(job);
int expected = 1;
int actual = _jobRepository.Count();
expected.ShouldEqual(actual);
}
[Fact]
public void ShouldUpdateTheJob()
{
Job job = new Job();
_jobRepository.Add(job);
Job jobToUpdate = _jobRepository.GetById(job.Id);
jobToUpdate.JobTitle = "New Job";
_jobRepository.Update(jobToUpdate);
string expected = "New Job";
Job updatedJob = _jobRepository.GetById(job.Id);
expected.ShouldEqual(updatedJob.JobTitle);
}
[Fact]
public void ShouldDeleteTheJob()
{
Job job = new Job();
_jobRepository.Add(job);
_jobRepository.Remove(job);
int expected = 0;
expected.ShouldEqual(_jobRepository.Count());
}
/// <summary>
/// Xunit.Specifications sample.
/// </summary>
[Specification]
public void EmptyJobRepositorySpecs()
{
IRepository<Job> repository = null;
"Given a new job repository".Context(() => repository = new TestRepository<Job>());
"the repository is empty".Assert(() => repository.GetAll().ShouldBeEmpty());
}
/// <summary>
/// Xunit.Specifications sample.
/// </summary>
[Specification]
public void AddJobToRepositorySpecs()
{
IRepository<Job> repository = null;
Job job = null;
"Given a new job repository".Context(() => repository = new TestRepository<Job>());
"when I add a new job".Do(() => { job = new Job(); repository.Add(job); });
"then the repository should have one job".Assert(() => 1.ShouldEqual(repository.Count()));
"and the job in the repository should be the job I created".Assert(() => job.ShouldBeSameAs(repository.GetById(job.Id)));
}
/// <summary>
/// NBehave sample.
/// </summary>
[Story, Fact]
public void AddingJobShouldIncreaseTheRepositoryCountByOne()
{
IRepository<Job> repository = null;
Job job = null;
Story story = new Story("Adding a job to the repository");
story.AsA("User")
.IWant("to the repository count to increment when I add a new job")
.SoThat("the repository count is accurate");
story.WithScenario("Add a job")
.Given("An empty repository", () => repository = new TestRepository<Job>())
.And("a new job", () => job = new Job())
.When("I add the new job to the repository", () => repository.Add(job))
.Then("the count of jobs in the repository should be $count", 1, count => count.ShouldEqual(repository.Count()));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment