简体   繁体   中英

FluentAssertions: string does not contain a definition for ShouldBeEquivalentTo

I am trying to use Nspec. I have followed these instructions: http://nspec.org/

  1. Create a class library project
  2. Nuget: Install-Package nspec
  3. Nuget: Install-Package FluentAssertions
  4. Create a class file and paste the following code:

using NSpec;
using FluentAssertions;

class my_first_spec : nspec
{
    string name;

    void before_each()
    {
        name = "NSpec";
    }

    void it_asserts_at_the_method_level()
    {
        name.ShouldBeEquivalentTo("NSpec");
    }

    void describe_nesting()
    {
        before = () => name += " Add Some Other Stuff";

        it["asserts in a method"] = () =>
        {
            name.ShouldBeEquivalentTo("NSpec Add Some Other Stuff");
        };

        context["more nesting"] = () =>
        {
            before = () => name += ", And Even More";

            it["also asserts in a lambda"] = () =>
            {
                name.ShouldBeEquivalentTo("NSpec Add Some Other Stuff, And Even More");
            };
        };
    }
}

The editor recognises the namespaces and the nspec class, however I see a compiler error that says:

'string does not contain a definition for ShouldBeEquivalentTo'.

What is the problem?

I am using .NET 4.7.1 and Visual Studio 2017.

I have spent some time Googling this and I have looked here for example:https://github.com/fluentassertions/fluentassertions/issues/234

FluentAssertions has removed ShouldBeEquivalentTo extension as a breaking change in more recent versions.

Refer to the recent FluentAssertions documentation for the suggested alternative

https://fluentassertions.com/introduction

name.Should().BeEquivalentTo(...);

Your example code would need to be updated to

class my_first_spec : nspec {
    string name;

    void before_each() {
        name = "NSpec";
    }

    void it_asserts_at_the_method_level() {
        name.Should().BeEquivalentTo("NSpec");
    }

    void describe_nesting() {
        before = () => name += " Add Some Other Stuff";

        it["asserts in a method"] = () => {
            name.Should().BeEquivalentTo("NSpec Add Some Other Stuff");
        };

        context["more nesting"] = () => {
            before = () => name += ", And Even More";

            it["also asserts in a lambda"] = () => {
                name.Should().BeEquivalentTo("NSpec Add Some Other Stuff, And Even More");
            };
        };
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM