Re: [nsubstitute] Linq expression argument matching

1,271 views
Skip to first unread message

David Tchepak

unread,
Mar 13, 2013, 4:50:31 PM3/13/13
to nsubs...@googlegroups.com
I don't know a good way of doing this, but what I tend to do is separate testing how the output from Get is used from how it is called (the predicate used).

So I'd start off with something like this:

[Test] public void ShouldUseObject() {
  var queryResult = new EnumerableQuery<MyObject>(new[] { new MyObject() };
  mockRepository.Get(null).ReturnsForAnyArgs(queryResult);
  Subject.DoStuffThatCallsRepository();
  // assert queryResult was used as expected
}

Then add a test for the predicate used:

[Test] public void QueryUsed() {
  Func<MyObject,bool> queryUsed = null;
  mockRepository.Get(Arg.Do<Expression<Func<MyObject,bool>>>(x => queryUsed = x.Compile());
  Subject.DoStuffThatCallsRepository();

  // test that queryUsed() predicate correctly matches/fails to match the required MyObject instances
}

A related change you could make is to expose the queries as properties somewhere and test them separately.

Hope this helps.

Regards,
David  



On Thu, Mar 14, 2013 at 1:22 AM, <andy.re...@gmail.com> wrote:
Hi,

I hope someone may be able to help answer a question I have. This is the first time I've needed to match a complex argument. I need to mock a repository method 'Get'. The method takes a Linq expression predicate. At the moment I have it matching any expression as follows:

    mockRepository.Get(Arg.Any<Expression<Func<MyObject, bool>>>())
                  .Returns(new EnumerableQuery<MyObject> ( new [] { new MyObject() } ); 

What I really need to do is perform a more detailed argument match based upon properties of the predicate object. I had initially thought the following would work but it obviously isn't so simple.

    mockRepository.Get(obj => obj.MyProperty == "MyValue")
                  .Returns(new EnumerableQuery<MyObject> ( new [] { new MyObject() } ); 

Any advice would be very much appreciated.

Andy.

--
You received this message because you are subscribed to the Google Groups "NSubstitute" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nsubstitute...@googlegroups.com.
To post to this group, send email to nsubs...@googlegroups.com.
Visit this group at http://groups.google.com/group/nsubstitute?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

William Green

unread,
Mar 13, 2013, 10:27:29 PM3/13/13
to nsubs...@googlegroups.com
Sounds like a repository to me. The whole idea behind a repository is to make working with a data store just like working with an in-memory collection.

If you're not testing the repository itself (and it doesn't seem like you are or want to), then create a custom implementation of your repository that is backed by a Collection, and use that in your test. Then, populate that collection as you see fit for your test. 

That's the way I've handled it in the past (until I stopped using repositories, that is).
Reply all
Reply to author
Forward
0 new messages