Skip to content

Instantly share code, notes, and snippets.

@mattflo
Created April 12, 2011 22:35
Show Gist options
  • Save mattflo/916594 to your computer and use it in GitHub Desktop.
Save mattflo/916594 to your computer and use it in GitHub Desktop.
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using CodeSlice.UnitTesting.Model;
using NSpec;
namespace CodeSlice.UnitTesting.NSpec
{
class describe_Vote : nspec
{
void when_validating_a_valid_vote()
{
new[] { -1, 1 }.Do(vote =>
it["of {0}, should be no errors".With(vote)] = () => Validate(vote).should_be_empty()
);
}
void when_validating_an_invalid_vote()
{
new[] { -2, 0, 2 }.Do(vote =>
it["of {0}, should be errors".With(vote)] = () => Validate(vote).should_not_be_empty()
);
}
IEnumerable<ValidationResult> Validate(int value)
{
Vote _vote = new Vote { Value = value };
List<ValidationResult> _results = new List<ValidationResult>();
ValidationContext _ctx = new ValidationContext(_vote, null, null);
Validator.TryValidateObject(_vote, _ctx, _results, true);
return from result in _results
where result.MemberNames.Contains("Value")
select result;
}
// output:
// describe Vote
// when validating a valid vote
// of -1, should be no errors
// of 1, should be no errors
// when validating an invalid vote
// of -2, should be errors
// of 0, should be errors
// of 2, should be errors
//
// 5 Examples, 0 Failed, 0 Pending
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment