Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SameAs in ShouldFluent doesn't work without cast on classes that implement IEnumerable<KeyValuePair> #1

Closed
chronodm opened this issue Mar 13, 2013 · 3 comments

Comments

@chronodm
Copy link

If you have something like:

class Foo : IEnumerable<KeyValuePair<Bar, Baz> { /* ... */ }

and you write a test like:

var foo1 = new Foo();
var foo2 = new Foo();
foo2.Should().Not.Be.SameAs(foo1);

you won't be able to get to SameAs(). I haven't drilled all the way down to find out why, but it looks like it starts with Should() returning ShouldDictionary. You can work around it by casting foo2 to Object, but it's a little awkward.

(Great library overall, though!)

@timscott
Copy link
Collaborator

I'm a little rusty on .NET so I might not remember correctly. SameAs means two variables point to the same instance of a reference typed object. Is it meaningful/useful to ask if one IEnumerable is the same as another (as opposed to individual members)?

@chronodm
Copy link
Author

Sure it is -- whatever implements IEnumerable is just an object.

class MyMap<K,V> : IEnumerable<KeyValuePair<K,V>> {
    /* ... various implementation details ... */

    /// <summary>Returns a deep copy of this map</summary>
    public MyMap<K,V> DeepCopy() {
    }
}

class MyMapTest {

    [Test]
    public void DeepCopyCopiesContents() {
        /* .... */
    }

    [Test]
    public void DeepCopyDoesNotShareContentReferences() {
         /* ... */
    }

    [Test]
    public void DeepCopyIsDifferentObject() {
        var map1 = CreateTestMap();
        var map2 = map1.DeepCopy();
        map2.Should().Not.Be.SameAs(map1); // doesn't work unless you cast map2 to Object
    }

    [Test]
    public void WritesToDeepCopyDoNotAffectOriginal() {
        /* ... even if above test passes, they might share internal state... */
    }
}

Just because the object under test implements an interface doesn't mean the usual assertions shouldn't be valid on it.

@timscott
Copy link
Collaborator

I see. Yes, it does tell you something, but nothing too valuable You're verifying that your code does not simply make an assignment and call it a copy. We didn't make every possible assertion available to every type. I personally would not write that test, but that's a matter of opinion. If you prefer not to cast to an object, we'd be happy to take a pull request. Should be pretty simple to add.

eloekset pushed a commit to eloekset/Should that referenced this issue Feb 26, 2017
VS2017 & netstandard1.6 support from @eloekset
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants