Skip to content

Instantly share code, notes, and snippets.

@benfoster
Created April 18, 2012 21:37
Show Gist options
  • Save benfoster/2416766 to your computer and use it in GitHub Desktop.
Save benfoster/2416766 to your computer and use it in GitHub Desktop.
Vacancy State Specs
public class VacancyStateTransitions
{
public class When_Submitting_a_new_vacancy : WithDomainEvents
{
static Vacancy vacancy;
Because of = ()
=> vacancy = new Vacancy(1);
It Should_be_unapproved = ()
=> vacancy.State.ShouldBeOfType<Unapproved>();
It Should_send_me_a_notification = ()
=> ReceivedEvent<VacancySubmittedEvent>().ShouldBeTrue();
}
[Subject("Opening a vacancy")]
public class Opening_a_vacancy : WithDomainEvents
{
static Vacancy vacancy;
static Exception exception;
public class When_it_is_unapproved
{
Establish ctx = ()
=> vacancy = new Vacancy(1);
Because of = ()
=> vacancy.Open();
It Should_be_open = ()
=> vacancy.State.ShouldBeOfType<Open>();
It Should_send_me_a_notification = ()
=> ReceivedEvent<VacancyOpenedEvent>().ShouldBeTrue();
}
public class When_it_is_not_unapproved // should really have tests for each state i.e. when it is already open
{
Establish ctx = ()
=> {
vacancy = new Vacancy(1);
vacancy.Open();
};
Because of = ()
=> exception = Catch.Exception(() => vacancy.Open());
It Should_fail = ()
=> exception.ShouldBeOfType<InvalidOperationException>();
}
}
[Subject("Closing a vacancy")]
public class Closing_a_vacancy
{
static Vacancy vacancy;
static Exception exception;
public class When_it_is_unapproved
{
Establish ctx = () =>
{
vacancy = new Vacancy(1);
};
Because of = ()
=> vacancy.Close("reason");
It Should_be_closed = ()
=> vacancy.State.ShouldBeOfType<Closed>();
}
public class When_it_is_open_and_I_close_it
{
Establish ctx = () =>
{
vacancy = new Vacancy(1);
vacancy.Open();
};
Because of = ()
=> vacancy.Close("reason");
It Should_be_closed = ()
=> vacancy.State.ShouldBeOfType<Closed>();
}
public class When_it_is_open_and_the_candidate_closes_it : WithDomainEvents
{
Establish ctx = () =>
{
vacancy = new Vacancy(1);
vacancy.Open();
};
Because of = ()
=> vacancy.Close("reason", closedInternally: true);
It Should_be_closed = ()
=> vacancy.State.ShouldBeOfType<Closed>();
It Should_send_me_a_notification = ()
=> LastEvent<VacancyClosedEvent>().ClosedInternally.ShouldBeTrue();
}
public class When_it_has_running_campaigns
{
Establish ctx = () =>
{
vacancy = new Vacancy(1);
vacancy.Open();
vacancy.RunNewCampaign(TestData.GetTestWebsiteCampaign());
};
Because of = ()
=> vacancy.Close("reason");
It Should_stop_all_campaigns = ()
=> vacancy.Campaigns.Any(c => c.Status == CampaignStatus.Active).ShouldBeFalse();
}
public class When_it_is_already_closed
{
Establish ctx = () =>
{
vacancy = new Vacancy(1);
vacancy.Open();
vacancy.Close("reason");
};
Because of = () => exception = Catch.Exception(() => vacancy.Close("reason"));
It Should_fail = ()
=> exception.ShouldBeOfType<InvalidOperationException>();
}
public class When_it_is_filled
{
Establish ctx = () =>
{
vacancy = new Vacancy(1);
vacancy.Open();
vacancy.Fill(new[] { new CandidateReference(1, "Ben Foster") });
};
Because of = ()
=> exception = Catch.Exception(() => vacancy.Close("reason"));
It Should_fail = ()
=> exception.ShouldBeOfType<InvalidOperationException>();
}
}
[Subject("Filling a vacancy")]
public class Filling_a_vacancy
{
static Vacancy vacancy;
static Exception exception;
public class When_it_is_not_open
{
Establish ctx = () =>
{
vacancy = new Vacancy(1);
};
Because of = ()
=> exception = Catch.Exception(() => vacancy.Fill(new[] { new CandidateReference(1, "Ben Foster") }));
It Should_fail = ()
=> exception.ShouldBeOfType<InvalidOperationException>();
}
public class When_it_is_open_with_running_campaigns
{
Establish ctx = () =>
{
vacancy = new Vacancy(1);
vacancy.Open();
vacancy.RunNewCampaign(TestData.GetTestWebsiteCampaign());
};
Because of = ()
=> exception = Catch.Exception(() => vacancy.Fill(new[] { new CandidateReference(1, "Ben Foster") }));
It Should_fail = ()
=> exception.ShouldBeOfType<InvalidOperationException>();
}
public class When_it_is_open_with_no_appointed_candidates
{
Establish ctx = () =>
{
vacancy = new Vacancy(1);
vacancy.Open();
};
Because of = ()
=> exception = Catch.Exception(() => vacancy.Fill(Enumerable.Empty<CandidateReference>()));
It Should_fail = ()
=> exception.ShouldBeOfType<InvalidOperationException>();
}
public class When_it_is_open_with_appointed_candidates_and_no_running_campaigns : WithDomainEvents
{
Establish ctx = () =>
{
vacancy = new Vacancy(1);
vacancy.Open();
};
Because of = ()
=> vacancy.Fill(new[] { new CandidateReference(1, "Ben Foster") });
It Should_be_filled = ()
=> vacancy.State.ShouldBeOfType<Filled>();
It Should_send_me_a_notification = ()
=> ReceivedEvent<VacancyFilledEvent>().ShouldBeTrue();
}
}
}
[Subject("Vacancy State Rules")]
public class VacancyStateAssertions
{
public class When_a_vacancy_is_unapproved
{
static Vacancy vacancy;
Because of = ()
=> vacancy = new Vacancy(1);
It Should_not_allow_me_to_run_a_campaign = ()
=> vacancy.State.CanRunNewCampaign().ShouldBeFalse();
It Should_not_allow_candidates_to_apply = ()
=> vacancy.State.CanRecieveApplications().ShouldBeFalse();
It Can_be_opened = ()
=> vacancy.State.CanBeOpened().ShouldBeTrue();
It Can_be_closed_by_me = ()
=> vacancy.State.CanBeClosed(internalRequest: true).ShouldBeTrue();
It Can_not_be_filled = ()
=> vacancy.State.CanBeFilled().ShouldBeFalse();
}
public class When_a_vacancy_is_open
{
static Vacancy vacancy;
Establish ctx = ()
=>
{
vacancy = new Vacancy(1);
vacancy.Open();
};
It Should_allow_me_to_run_campaigns = ()
=> vacancy.State.CanRunNewCampaign().ShouldBeTrue();
It Can_be_closed_by_me_or_the_client = () =>
{
vacancy.State.CanBeClosed(true).ShouldBeTrue();
vacancy.State.CanBeClosed(false).ShouldBeTrue();
};
It Can_not_be_opened = ()
=> vacancy.State.CanBeOpened().ShouldBeFalse();
[Subject("When a vacancy is open")]
public class When_the_vacancy_has_running_campaigns
{
Because of = ()
=> vacancy.RunNewCampaign(TestData.GetTestWebsiteCampaign());
It Should_allow_candidates_to_apply =
() => vacancy.State.CanRecieveApplications().ShouldBeTrue();
It Can_not_be_filled = ()
=> vacancy.State.CanBeFilled().ShouldBeFalse();
}
[Subject("When a vacancy is open")]
public class When_the_vacancy_has_no_running_campaigns
{
It Should_not_allow_candidates_apply = ()
=> vacancy.State.CanRecieveApplications().ShouldBeFalse();
It Can_be_filled = ()
=> vacancy.State.CanBeFilled().ShouldBeTrue();
}
public class When_a_vacancy_is_closed
{
static Vacancy vacancy;
Establish ctx = () =>
{
vacancy = new Vacancy(1);
vacancy.Open();
vacancy.Close("reason");
};
It Should_not_allow_me_to_run_a_new_campaign = ()
=> vacancy.State.CanRunNewCampaign().ShouldBeFalse();
It Should_not_allow_candidates_to_apply = ()
=> vacancy.State.CanRecieveApplications().ShouldBeFalse();
It Can_not_be_reopened = ()
=> vacancy.State.CanBeOpened().ShouldBeFalse();
It Can_not_be_filled = ()
=> vacancy.State.CanBeFilled().ShouldBeFalse();
It Can_not_be_closed = ()
=> vacancy.State.CanBeClosed().ShouldBeFalse();
}
public class When_a_vacancy_is_filled
{
static Vacancy vacancy;
Establish ctx = () =>
{
vacancy = new Vacancy(1);
vacancy.Open();
vacancy.Fill(new[] { new CandidateReference(1, "Ben Foster") });
};
It Should_not_allow_me_to_run_a_new_campaign = ()
=> vacancy.State.CanRunNewCampaign().ShouldBeFalse();
It Can_not_be_opened = ()
=> vacancy.State.CanBeOpened().ShouldBeFalse();
It Can_not_be_closed = ()
=> vacancy.State.CanBeClosed().ShouldBeFalse();
It Can_not_be_filled = ()
=> vacancy.State.CanBeFilled().ShouldBeFalse();
}
}
}
@benfoster
Copy link
Author

Output:

When Submitting a new vacancy
¯ Should be unapproved
¯ Should send me a notification

Opening a vacancy, When it is unapproved
¯ Should be open
¯ Should send me a notification

Opening a vacancy, When it is not unapproved
¯ Should fail

Closing a vacancy, When it is unapproved
¯ Should be closed

Closing a vacancy, When it is open and I close it
¯ Should be closed

Closing a vacancy, When it is open and the candidate closes it
¯ Should be closed
¯ Should send me a notification

Closing a vacancy, When it has running campaigns
¯ Should stop all campaigns

Closing a vacancy, When it is already closed
¯ Should fail

Closing a vacancy, When it is filled
¯ Should fail

Filling a vacancy, When it is not open
¯ Should fail

Filling a vacancy, When it is open with running campaigns
¯ Should fail

Filling a vacancy, When it is open with no appointed candidates
¯ Should fail

Filling a vacancy, When it is open with appointed candidates and no running campaigns
¯ Should be filled
¯ Should send me a notification

Vacancy State Rules, When a vacancy is unapproved
¯ Should not allow me to run a campaign
¯ Should not allow candidates to apply
¯ Can be opened
¯ Can be closed by me
¯ Can not be filled

Vacancy State Rules, When a vacancy is open
¯ Should allow me to run campaigns
¯ Can be closed by me or the client
¯ Can not be opened

When a vacancy is open, When the vacancy has running campaigns
¯ Should allow candidates to apply
¯ Can not be filled

When a vacancy is open, When the vacancy has no running campaigns
¯ Should not allow candidates apply
¯ Can be filled

Vacancy State Rules, When a vacancy is closed
¯ Should not allow me to run a new campaign
¯ Should not allow candidates to apply
¯ Can not be reopened
¯ Can not be filled
¯ Can not be closed

Vacancy State Rules, When a vacancy is filled
¯ Should not allow me to run a new campaign
¯ Can not be opened
¯ Can not be closed
¯ Can not be filled

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