xUnit Assert之其他

1. Contains test

[Fact]
public void CanFindNullInContainer()
{
var list = new List<object> { 16, null, "Hi there" };

Assert.Contains(null, list);
}
[Fact]
public void ListInContainer()
{
var list = new List<object> {1, 2, 3};
var list2 = new List<object> {4, 5, list};
Assert.Contains(list, list2);
}

2. NotContains Test

[Fact]
public void CanSearchForNullInContainer()
{
var list = new List<object> { 16, "Hi there" };
Assert.DoesNotContain(null, list);
}
[Fact]
public void NullContainerDoesNotThrow()
{
Assert.DoesNotThrow(() => Assert.DoesNotContain(14, null));
}
 
[Fact]
public void CanSearchForSubstringsCaseInsensitive()
{
Assert.Throws<DoesNotContainException>(
() => Assert.DoesNotContain("WORLD", "Hello, world!", StringComparison.InvariantCultureIgnoreCase));
}
3. Empty Test
[Fact]
public void IsEmpty()
{
var list = new List<int>();
Assert.Empty(list);
}
[Fact]
public void NullIsNotEmpty()
{
Assert.Throws<ArgumentNullException>(() => Assert.Empty(null));
}
[Fact]
public void IsEmpty()
{
Assert.Empty("");
}
4. False Test
[Fact]
public void AssertFalse()
{
Assert.False(false);
}
5. True Test
[Fact]
public void AssertTrue()
{
Assert.True(true);
}
5. Single Test
[Fact]
public void SingleItemCollectionReturnsTheItem()
{
var collection = new ArrayList { "Hello" };
var result = Assert.Single(collection);

Assert.Equal("Hello", result);
}
[Fact]
public void ObjectSingleMatch()
{
IEnumerable collection = new[] { "Hello", "World!" };

Assert.Single(collection, "Hello");
}
6. Same Test
[Fact]
public void BoxedTypesDontWork()
{
int index = 0;

Assert.Throws<SameException>(() => Assert.Same(index, index));
}
[Fact]
public void ValuesAreNotTheSame()
{
Assert.Throws<SameException>(() => Assert.Same("bob", "jim"));
}
[Fact]
public void ValuesAreTheSame()
{
const string jim = "jim";

Assert.Same(jim, jim);
}
7. IsType Test
[Fact]
public void IsType()
{
var expected = new InvalidCastException();
Assert.IsType(typeof(InvalidCastException), expected);
Assert.IsType<InvalidCastException>(expected);
}
8. InRange Test
[Fact]
public void DoubleValueWithinRange()
{
Assert.InRange(1.0, .75, 1.25);
}
[Fact]
public void StringValueWithinRange()
{
Assert.InRange("bob", "adam", "scott");
}
9. Throw Test
[Fact]
public void DoesNotThrowException()
{
var methodCalled = false;
Assert.DoesNotThrow(() => methodCalled = true);
Assert.True(methodCalled);
}

posted on 2011-11-26 14:13  蚂蚁蚂蚁  阅读(824)  评论(0编辑  收藏  举报

导航