惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

V
Visual Studio Blog
博客园 - 司徒正美
博客园_首页
Jina AI
Jina AI
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
月光博客
月光博客
I
InfoQ
M
MIT News - Artificial intelligence
T
Tailwind CSS Blog
L
LangChain Blog
Last Week in AI
Last Week in AI
A
About on SuperTechFans
B
Blog
博客园 - 叶小钗
雷峰网
雷峰网
H
Help Net Security
WordPress大学
WordPress大学
大猫的无限游戏
大猫的无限游戏
博客园 - 【当耐特】
云风的 BLOG
云风的 BLOG
Microsoft Azure Blog
Microsoft Azure Blog
小众软件
小众软件
aimingoo的专栏
aimingoo的专栏
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻

博客园 - yellowwood

cmder简单使用 回想一下2013中国软件技术大会 数字用千分位表示 从AD( Active Directory )读取 Email 报表产生方式之二比较 报表产生方式之一图示 报表订阅 報表部署 via VS2005 报表参数 报表rdl嵌入网页(ASP.NET) 報表与子報表 RDL之矩陣 [Oracle整理]数据类型大全 [Oracle整理].NET 调用 Oracle之REF Cursor [Oracle整理]Oracle游标(显示游标&隐式游标&动态游标&参数游标) [Oracle整理]Oracle之Procedure参数类型 [Oracle整理]Oracle之数组 [Oracle整理]Oracle之ROWTYPE和RECORD [Oracle整理]synonym及其应用
Should Assertion Library
yellowwood · 2014-03-16 · via 博客园 - yellowwood

Should Assertion Library,通常在测试时用到,可以与nunit 结合使用。

已经从codeplex 迁移到 github。网址如下

https://github.com/erichexter/Should

github 时常访问不了,这次能访问,主要内容先摘抄记录下,备查询。

Project Description

The Should Assertion Library provides a set of extension methods for test assertions for AAA and BDD style tests. It provides assertions only, and as a result it is Test runner agnostic. The assertions are a direct fork of the xUnit test assertions. This project was born because test runners Should be independent of the the assertions!

Should Assertion Library comes in two flavors, each with it's own binary.

Standard

Install from nuget.

PM> install-package should

The following example shows some of the assertions that are available for objects, booleans, string, and collections.

public void Should_assertions()
{
   object obj = null;
   obj.ShouldBeNull();

    obj = new object();
    obj.ShouldBeType(typeof(object));
    obj.ShouldEqual(obj);
    obj.ShouldNotBeNull();
    obj.ShouldNotBeSameAs(new object());
    obj.ShouldNotBeType(typeof(string));
    obj.ShouldNotEqual("foo");

    obj = "x";
    obj.ShouldNotBeInRange("y", "z");
    obj.ShouldBeInRange("a", "z");
    obj.ShouldBeSameAs("x");

    "This String".ShouldContain("This");
    "This String".ShouldNotBeEmpty();
    "This String".ShouldNotContain("foobar");

    false.ShouldBeFalse();
    true.ShouldBeTrue();

    var list = new List<object>();
    list.ShouldBeEmpty();
    list.ShouldNotContain(new object());

    var item = new object();
    list.Add(item);
    list.ShouldNotBeEmpty();
    list.ShouldContain(item);
}

Fluent

Should.Fluent is a direct port of ShouldIt. Install from nuget.

PM> install-package ShouldFluent

The following shows the same assertions as above but in the fluent style.

public void Should_fluent_assertions()
{
    object obj = null;
    obj.Should().Be.Null();

    obj = new object();
    obj.Should().Be.OfType(typeof(object));
    obj.Should().Equal(obj);
    obj.Should().Not.Be.Null();
    obj.Should().Not.Be.SameAs(new object());
    obj.Should().Not.Be.OfType<string>();
    obj.Should().Not.Equal("foo");

    obj = "x";
    obj.Should().Not.Be.InRange("y", "z");
    obj.Should().Be.InRange("a", "z");
    obj.Should().Be.SameAs("x");

    "This String".Should().Contain("This");
    "This String".Should().Not.Be.Empty();
    "This String".Should().Not.Contain("foobar");

    false.Should().Be.False();
    true.Should().Be.True();

    var list = new List<object>();
    list.Should().Count.Zero();
    list.Should().Not.Contain.Item(new object());

    var item = new object();
    list.Add(item);
    list.Should().Not.Be.Empty();
    list.Should().Contain.Item(item);
};

Here are some additional examples of assertions using the fluent API:

public void Should_fluent_assertions()
{
    var numbers = new List<int> { 1, 1, 2, 3 };
    numbers.Should().Contain.Any(x => x == 1);
    numbers
        .Should().Count.AtLeast(1)
        .Should().Count.NoMoreThan(5)
        .Should().Count.Exactly(4)
        .Should().Contain.One(x => x > 2);

    var id = new Guid();
    id.Should().Be.Empty();

    id = Guid.NewGuid();
    id.Should().Not.Be.Empty();

    var date = DateTime.Now;
    date1.Should().Be.Today();

    var str = "";
    str.Should().Be.NullOrEmpty();                

    var one = "1";
    one.Should().Be.ConvertableTo<int>();

    var idString = Guid.NewGuid().ToString();
    idString.Should().Be.ConvertableTo<Guid>();
}