First off, it looks like you're using a Service Locator.  The Service
Locator is generally regarded as an anti-pattern
(http://blog.ploeh.dk/2010/02/03/ServiceLocatorIsAnAntiPattern.aspx)
and you should try and avoid it if possible.  Ideally, the
IRepository<Package> would be injected into the class that contains
this method.  However, if you're stuck with this, there's still a way
you can test this.

Your method does two things:

1) Runs the LINQ query on the IRepository<Package>
2) Calls the Package.Set() method

For #1, I would create a stub IRepository<Package> and stub the
GetQuery() method to return a set of data that will "exercise" your
LINQ query.  In other words, make sure it returns data that will test
both true and false for your Where() condition.  And don't forget to
test the possibility that there are no matches and you return null
(these would all be separate tests).

For #2, you have a couple of options.  If you can create a mock of
"Package" (if it's a property, it must be virtual), you can verify
that the "Set" method was called with the particular package that you
expected your LINQ query to return.  If Package is NOT virtual and you
can't change it, then your test would have to do some kind of
verification after Execute(int PackageID) was called to make a guess
that "Set" was called (perhaps by checking other properties).

Hope this helps.

---
Patrick Steele
http://weblogs.asp.net/psteele



On Fri, Jan 20, 2012 at 12:01 AM, Laksh <[email protected]> wrote:
> I'm using 3.6 version of Rhino. How do i test the following funtion
> which has LINQ query
>
> protected override void Execute(int PackageID)
>        {
>
>            // get the package hierarchy from the database
>            IRepository<Package> repository =
> DefaultServiceLocator.Instance.GetInstance<IRepository<Package>>();
>            var package = repository.GetQuery()
>                .Where(a => a.PackageID == PackageID)
>                .Include(a => a.PackageDetails.Select(b =>
> b.DocumentTemplate))
>                .Include(a => a.PackageArea)
>                .Include(a => a.ReturnDetails)
>                .FirstOrDefault();
>
>                 this.Package.Set(package);
>        }
>
> --
> You received this message because you are subscribed to the Google Groups 
> "Rhino.Mocks" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to 
> [email protected].
> For more options, visit this group at 
> http://groups.google.com/group/rhinomocks?hl=en.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Rhino.Mocks" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rhinomocks?hl=en.

Reply via email to