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

Recursive property setup overrides previous setups #110

Closed
jamesfoster opened this issue May 12, 2014 · 7 comments
Closed

Recursive property setup overrides previous setups #110

jamesfoster opened this issue May 12, 2014 · 7 comments
Assignees
Labels
Milestone

Comments

@jamesfoster
Copy link

Apologies for the cryptic subject. It's difficult to explain this without an example.

Here's a failing test:

var baz = Mock.Of<Baz>(x => x.Value == "beforeBaz");
var qux = Mock.Of<Qux>(x => x.Value == "beforeQux");

var bar = Mock.Of<Bar>(x =>
    x.Baz == baz &&
    x.Qux == qux);

var obj = Mock.Of<Foo>(x => x.Bar == bar);

var mock = Mock.Get(obj);

obj.Bar.Baz.Value.ShouldBe("beforeBaz"); // Pass
obj.Bar.Qux.Value.ShouldBe("beforeQux"); // Pass

mock.SetupGet(x => x.Bar.Baz.Value).Returns("test");

obj.Bar.Baz.Value.ShouldBe("test"); // Pass
obj.Bar.Qux.Value.ShouldBe("beforeQux"); // Fail

Effectively I've set up the return value of Foo.Bar, Bar.Qux and Qux.Value separately and then used the call the SetupGet to override the value of Foo.Bar.Baz.Value. This replaces the previously setup Foo.Bar with a new mock without the registration of Bar.Qux.

Not sure whether this is an edge case you want to support but I'm not sure of the best way to work around it. Any help would be greatly appreciated

@jamesfoster
Copy link
Author

For completeness the interfaces are defined as:

    public interface Foo
    {
        Bar Bar { get; }
    }

    public interface Bar
    {
        Baz Baz { get; }
        Qux Qux { get; }
    }

    public interface Qux
    {
        string Value { get; }
    }

    public interface Baz
    {
        string Value { get; }
    }

@kzu
Copy link
Contributor

kzu commented May 15, 2014

Seems like a valid use case and a bug :(

@jamesfoster
Copy link
Author

It gets awkward when you replace bar with

  var bar = new BarImpl {
    Baz = baz,
    Qux = qux
  }

If the recursive syntax ends on a mock it should still work.

I've solved my issue by writing a method that takes a Func<T, TMocked> to get an IMocked, and an Expression<Func<TMocked, TProperty>> to pass to SetupGet.

Do you want me to have a look at fixing this?

@kzu
Copy link
Contributor

kzu commented May 15, 2014

Would love it if you could give it a shot!

Thanks

/kzu

On Thu, May 15, 2014 at 3:27 PM, James Foster notifications@github.comwrote:

It gets awkward when you replace bar with

var bar = new BarImpl {
Baz = baz,
Qux = qux
}

If the recursive syntax ends on a mock it should still work.

I've solved my issue by writing a method that takes a Func<T, TMocked> to
get an IMocked, and an Expression<Func<TMocked, TProperty>> to pass to
SetupGet.

Do you want me to have a look at fixing this?

Reply to this email directly or view it on GitHubhttps://github.com//issues/110#issuecomment-43246948
.

@jamesfoster
Copy link
Author

Cool. I'll check it out. I came across the relevant areas of the code when making my last pull request. Shouldn't be too difficult. (He says)

@stakx
Copy link
Contributor

stakx commented Jul 13, 2018

Closing this, this problem will be tracked in #643 (together with a couple other related issues).

@stakx stakx closed this as completed Jul 13, 2018
@stakx stakx reopened this Mar 10, 2019
@stakx
Copy link
Contributor

stakx commented Mar 10, 2019

After the recent merges, this might finally be fairly easy to fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants