FluentAssertions: string does not contain a definition for ShouldBeEquivalentTo
Asked Answered
H

1

5

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

Hindquarter answered 6/3, 2018 at 10:17 Comment(3)
@CodeCaster, could you clarify what you mean. I am just following the instructions. The code is on the webpage I link to.Hindquarter
Well, that sample is wrong/outdated. It happens. In recent versions of FluentAssertions, that's written .Should().BeEquivalentTo().Spearman
github.com/nspec/NSpec/issues/161Kilovolt
F
17

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");
            };
        };
    }
}
Frequent answered 6/3, 2018 at 10:52 Comment(6)
Thanks. That works as expected. Would you put your Nspec tests in a separate Visual Studio project to your Specflow scenarios?Hindquarter
can you answer the question in my comment above? Then I will mark your answer.Hindquarter
@w0051977: That's not how stack overflow works! If you have another question, post a new one. This is not a forum, tutoring or discussion site. If the answer solved your original question, accept it. Please take the mandatory tour again to recap site-rules.Burkholder
Ahh... this works for Strings but doesn't seem to work for Int's. Seems Ints is .Should().Be(...)Dulcy
@Frequent - I was using ShouldBeEquivalentTo to compare lists of custom types, it was nicely comparing object public properties, wondering if you could suggest how to do this when ShouldBeEquivalentTo is not availablle anymore?Cockfight
@Cockfight .Should().BeEquivalentTo should (pardon the pun) work for your assertions as well.Frequent

© 2022 - 2024 — McMap. All rights reserved.