Ok, here's an approach I would take.
You want to make sure that your FooProvider will not return any of the
blacklisted items. That's the behavior you want to test. Here's how
I would see as one approach to testing this. For simplicity, I'll use
strings instead of Foo instances, but the concept is the same. I also
assume you've got some kind of a repository layer that you can stub
out so you're not hitting the DB during your unit tests.
// arrange
IRepository repository = MockRepository.GenerateStub<IRepository>();
var fooProvider = new FooProvider(repository);
fooProvider.BlackList = new [] { "A", "E", "I" };
repository.Stub(r => r.GetNext10Items()).Return(new [] { "A", "B",
"C", "D", "E"});
// act
var result = fooProvider.GetItems(); // internally calls
repository.GetNext10Items()
// assert
Assert.AreEqual(3, result.Count);
Assert.IsTrue(result.Contains("B");
Assert.IsTrue(result.Contains("C");
Assert.IsTrue(result.Contains("D");
This only tests the behavior of the "GetItems" method (that items
returned from the repository but are contained in the blacklist are
not returned to the caller). You can change your repository
implementation as well as the "GetItems" implementation without
breaking your test. Suppose you change the BlackList to be a small
in-memory SQLite database instead of a List<T>? Now your test breaks
because you wouldn't be using List<T> anymore. This is an
implementation detail that shouldn't leak into your unit tests.
Hope this helps!
---
Patrick Steele
http://weblogs.asp.net/psteele
On Thu, May 5, 2011 at 3:21 AM, Giulio Petrucci
<[email protected]> wrote:
> Hi Patrick,
>
> On Wed, May 4, 2011 at 3:44 PM, Patrick Steele <[email protected]>
> wrote:
>> Why create a StrictMock of List<T>?
>
> To test the interaction of a class with onw of its inner members.
>
>> For one thing, the Contains
>> method isn't virtual so you can't set expectations on it (nor stub
>> it's behavior).
>
> Got it.
>
>> Second, I tend to avoid StrictMock's.
> [cut]
>
> Uhm... could you suggest me an alternative?
>
>> What's the goal of what you're trying to test? What class is being
>> tested in this scenario?
>
> I have a class called FooProvider which implements some selection
> logic on a database table Foo. The FooProvider instance holds a
> blacklist (which is the List<Foo> I wrote before) so when I ask it
> "give me the next 10 Foo instances" the provider run a query over the
> database and exclde all the blacklisted ones.
>
> Thanks again,
> Giulio
>
> --
>
> --
> 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.