views:

47

answers:

1

I've got a method that's mildly complicated and needs to be very well tested. Secret sauce stuff. Ok, maybe not that cool, but I'm not 100% sure how to go about getting these things setup. This sort of stems from my previous question here. I haven't used rhino mocks so I'm still bad/unaware of the syntax, so feel free to make a ton of suggestions.

Public Function GenerateAllNotifications(ByVal days As List(Of Integer)) As List(Of MailMessage) Implements INotificationService.GenerateAllNotifications

     Dim someStuff = _someService.GetThingsThatExpireBetween(day1, day2)

     'build some messages

     Return messages

End Function

My setup in my tests are looking like this ... I know this is wrong though

Dim fakeStuff = New SomeItem()

Dim fakeContext = New List(Of Provider)
fakeContext.Add(fakeStuff)
Dim someService = MockRepository.GenerateStub(Of ISomeService)()
someService.Stub(Function(x) x.GetThingsThatExpireBetween(30, 60)).IgnoreArguments().Return(fakeContext.AsQueryable)
_fakeNotificationService = New NotificationService(someService)

What I want to accomplish is returning an execpted items out of that service. The business rule is a collection of messages based on that given expiration. So if a given entity is expiring in 30 days, the message reflects that, if it's 60, 90, whatever, those are put in the messages. I think my problem is I need to have different objects come back (someitem) each time ... ?

A: 

It seems as if you're trying to make your mock behave correctly. If you think of your mock instead as providing the context for the behaviour of your class under test, it might help.

  • Given my mock will behave a certain way
  • When I use my class
  • Then I expect this outcome

Then you can make your mock behave different ways - regardless of the arguments - and check that your class behaves appropriately in those different situations. You will have a new test for each context. I often name my tests after the behaviour in the contexts:

  • should give me an empty list if no things are found
  • should process and return the things which are found
  • should provide an error message if the service throws an exception

etc., only camel-cased and with a [Test] attribution.

Does this help? If not, I may have misunderstood the problem - apologies.

Lunivore