Of course!  

 

Dip in to the concrete class's method and run the expectation on the
already mocked out dependency.  

 

Thanks a lot.


Don

 

From: [email protected] [mailto:[email protected]]
On Behalf Of Tim Barcz
Sent: Friday, February 06, 2009 12:44 PM
To: [email protected]
Subject: [RhinoMocks] Re: Can I mock a class and pass it mocked
dependencies as constructor args?

 

So very close!!!

Change:

invoiceSvc. AssertWasCalled(x => x.Save(Arg<Invoice>.Is.Anything));  //
not mocked.. won't work

TO

repository. AssertWasCalled(x => x.Save(Arg<Invoice>.Is.Anything));

You should be good to go.

On Fri, Feb 6, 2009 at 10:17 AM, Cote, Don <[email protected]> wrote:

This might be rudimentary, but I'm having trouble figuring it out.  And
if this question has been posed a zillion times, just point me to the
resource that explains it and I'll take it from there.  Many thanks in
advance!  -- Don

 

 

I have a service class for my invoice entity.  I'm injecting a
repository object in through its constructor... pretty straightforward
stuff (see down at the bottom).

 

Now I want to be able to test a ProcessStatusChange method, which does
some business logic processing and eventually calls a private Save
method, which relies on the repository field.  What I ultimately want to
do is to assert that the Save method was called. 

 

So... I would like to avoid using my IoC container in my tests, but is
there a way to mock out my service class, but also pass it my mocked out
repository as well?  Let me know if this needs any more clarification.

 

Here are my takes on how to do this.  The first one definitely won't
work because the invoiceSvc isn't being mocked.  The second one doesn't
work because there's no repository object in invoiceSvc, since it was
mocked out.  

 

[Test]

public void ProcessStatusChangeTest-Version1()  

{

    var repository =
MockRepository.GenerateMock<IRepository<Invoice>>();

    var invoiceSvc = new InvoiceService(_repository);

 

    invoiceSvc.ProcessStatusChange(new Invoice());

    

    invoiceSvc. AssertWasCalled(x => x.Save(Arg<Invoice>.Is.Anything));
// not mocked.. won't work

}

 

[Test]

public void ProcessStatusChangeTest-Version2()

{

    var invoiceSvc = MockRepository.GenerateMock<IInvoiceService>();

    

    invoiceSvc.ProcessStatusChange(new Invoice());

 

    invoiceSvc. AssertWasCalled(x => x.Save(Arg<Invoice>.Is.Anything));

 

    // ^^ this fails because the InvoiceService doesn't have an
instantiated IRepository<Invoice>

}

 

 

public class InvoiceService : IInvoiceService

{

    private readonly IRepository<Invoice> _repository;

 

    public InvoiceService(IRepository<Invoice> repository)

    {

        _repository = repository;

    }

    

    public void ProcessStatusChange(Invoice invoice)

    {

        .... some business logic....

        

        this.Save(invoice);

    }

    

    private Save(Invoice invoice)

    {

       _repository.Save(invoice);

    }

}

 

 

Don Cote 
Kenexa(r)
HIRING & RETENTION
Outsourcing | Employee Research | Software 


Office: 781-530-5049

Mail: 343 Winter Street, Waltham, MA 02451

 

 

 





--~--~---------~--~----~------------~-------~--~----~
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