Problem with stubbing generic method

83 views
Skip to first unread message

Ku buś

unread,
Mar 21, 2014, 7:26:44 AM3/21/14
to rhino...@googlegroups.com
Hello,
Iam having very weird problem. I searched through web and couldnt find any solution. I found something similar here: https://www.mail-archive.com/rhino...@googlegroups.com/msg00656.html
but it didnt help in my case.

public interface IConnectionDecorator
{
    ResultType RunWithDecorator<ResultType>(IDbCommand cmd, Func<IDbCommand, ResultType> f);
}

public class TransactionDecorator : IQueryResultDecorator
{
    private readonly IConnectionDecorator connectionDecorator;

    public TransactionDecorator(IConnectionDecorator connectionDecorator)
    {
        this.connectionDecorator = connectionDecorator;
    }

    public QueryResult<ResultType> RunWithDecorator<ResultType>(IDbCommand cmd, Func<IDbCommand, ResultType> func)
    {
        return connectionDecorator.RunWithDecorator(cmd, c => withTransaction(c, func));
    }



    private QueryResult<ResultType> withTransaction<ResultType>(IDbCommand c, Func<IDbCommand, ResultType> f)
    {

    }
}

I try to mock it like this:

var conDec = MockRepository.GenerateMock<IDbConnection>();
conDec.Stub(a => a.RunWithDecorator(cmd, c => 2)).IgnoreArguments().Do((Func<IDbCommand, Func<IDbCommand, int>, int>)((c, fun) =>
                                                                                    {
                                                                                        fun(c);
                                                                                        throw new Exception();
                                                                                        return 2;
                                                                                    }));

and when I call TransactionDecorator.RunWithDecorator my stubbed function is not called. Funny thing is that if I put something else instead of c=> withTransaction(c, func) in argument, everything works fine. Is it me doing something wrong or its some sort of bug ?

Regards,
Qba

haifisch

unread,
Mar 24, 2014, 12:24:16 PM3/24/14
to rhino...@googlegroups.com
Hi Qba,

I tried to reproduce the issue, but I've failed because I'm missing some context. Would you be so kind and delivery a small compilable code sample illustrating the problem? A unit test which could be included as a field problem into RhiniMocks code base would be perfect ;-)

Br,

Andreas

Mike Meisinger

unread,
Mar 24, 2014, 8:18:55 PM3/24/14
to rhino...@googlegroups.com
Agreed, we can add it to the field problems once we have more context.

Just from looking at the problem, however, I can't tell if there was a typo or not but the only thing that is being mocked is the IDbConnection rather than the decorator. Therefore, getting an actual unit test to look at would help clear that up and provide context.

Just for kicks, I would setup the mock as a Strict Mock. That way if anything is called that wasn't expected you will know. You could also try using a logger to get a better understanding of what is being called and what Rhino Mocks is doing.

Hope that helps

Marcelo Oliveira

unread,
Mar 24, 2014, 8:37:11 PM3/24/14
to rhino...@googlegroups.com
I had a couple of problems with similar scenarios... ended up using another mock library in these cases.



--
You received this message because you are subscribed to the Google Groups "Rhino.Mocks" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rhinomocks+...@googlegroups.com.
To post to this group, send email to rhino...@googlegroups.com.
Visit this group at http://groups.google.com/group/rhinomocks.
For more options, visit https://groups.google.com/d/optout.

Ku buś

unread,
Apr 1, 2014, 6:45:46 AM4/1/14
to rhino...@googlegroups.com
Thanks for answers, too bad you couldnt reproduce the issue. Anyway I dont need anymore help about the issue, becuase I used different aproach to implement my funcitonality and refactored the code, so that Iam not having the issue as I described above.

Qba
Reply all
Reply to author
Forward
0 new messages