Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
My method needs to be virtual?
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  7 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Philippe Lavoie  
View profile  
 More options Apr 28 2008, 12:58 pm
From: "Philippe Lavoie" <philippe.lav...@gmail.com>
Date: Mon, 28 Apr 2008 12:58:11 -0400
Local: Mon, Apr 28 2008 12:58 pm
Subject: My method needs to be virtual?
Hi

Do I really need to mark my methods as virtual if I want to mock them with moq?

I'd like the following two tests to work. What is the work around?

    public class Foo
    {
        public void Bar()
        {
        }

        public virtual void Bar2()
        {
        }
    }

    /// <summary>
    /// Summary description for UnitTest1
    /// </summary>
    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void ThisWorks()
        {
            bool called = false;
            Mock<Foo> fooMock = new Mock<Foo>();
            fooMock.Expect(foo => foo.Bar2()).Callback( () => called = true);
            fooMock.Object.Bar2();
            Assert.AreEqual(true,called);
        }

                [TestMethod]
        public void ThisDoesnt()
        {
            bool called = false;
            Mock<Foo> fooMock = new Mock<Foo>();
            fooMock.Expect(foo => foo.Bar()).Callback( () => called = true);
            fooMock.Object.Bar();
            Assert.AreEqual(true,called);
        }
    }

Thanks

Phil

PS Please include my e-mail in the response.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Nathan Stott  
View profile  
 More options Apr 28 2008, 1:00 pm
From: "Nathan Stott" <nrst...@gmail.com>
Date: Mon, 28 Apr 2008 12:00:49 -0500
Local: Mon, Apr 28 2008 1:00 pm
Subject: Re: [Moq] My method needs to be virtual?
Yes you really need to mark them virtual.  Dynamic Proxy can not
intercept method calls to non virtual, non abstract methods.

On Mon, Apr 28, 2008 at 11:58 AM, Philippe Lavoie


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Daniel Cazzulino  
View profile  
 More options Apr 28 2008, 6:30 pm
From: "Daniel Cazzulino" <dan...@cazzulino.com>
Date: Mon, 28 Apr 2008 19:30:49 -0300
Local: Mon, Apr 28 2008 6:30 pm
Subject: Re: [Moq] Re: My method needs to be virtual?

a "workaround" would be to define an interface for your object, and mock the
interface rather than the actual implementation (which may be a smell in
itself...)


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
philippe.lav...@gmail.com  
View profile  
 More options Apr 29 2008, 10:09 am
From: philippe.lav...@gmail.com
Date: Tue, 29 Apr 2008 07:09:21 -0700 (PDT)
Local: Tues, Apr 29 2008 10:09 am
Subject: Re: My method needs to be virtual?
Thanks

Is this a limitation in the framework or in .Net 3.5 ?

Phil


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Daniel Cazzulino  
View profile  
 More options Apr 29 2008, 12:10 pm
From: "Daniel Cazzulino" <dan...@cazzulino.com>
Date: Tue, 29 Apr 2008 13:10:51 -0300
Local: Tues, Apr 29 2008 12:10 pm
Subject: Re: [Moq] Re: My method needs to be virtual?

it's a limitation of the CLR since its inception: there's no built-in
interception mechanism.

so, the interception mechanism we use is auto-generated classes that inherit
from the types to mock, and override all members to provide the
interception. that's why they need to be virtual.

we're reusing the interception library from Castle DynamicProxy for doing
that.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
philippe.lav...@gmail.com  
View profile  
 More options Apr 29 2008, 10:11 pm
From: philippe.lav...@gmail.com
Date: Tue, 29 Apr 2008 19:11:42 -0700 (PDT)
Local: Tues, Apr 29 2008 10:11 pm
Subject: Re: My method needs to be virtual?

Thanks for the clarification. I guess we just have to hope that the
powers that be decide to change this in .Net 4.0.

Regards

Phil


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Daniel Cazzulino  
View profile  
 More options Sep 30 2008, 8:23 am
From: Daniel Cazzulino <dan...@cazzulino.com>
Date: Tue, 30 Sep 2008 09:23:23 -0300
Local: Tues, Sep 30 2008 8:23 am
Subject: Re: My method needs to be virtual?
To me, it's no smell.

Some may argue that being "forced" (you can always just mark the  
member virtual and that's it) to create an interface just for mock-
ability rather than architecture/design decision is a smell...

Sent from my iPod

On Sep 29, 2008, at 9:16 PM, panjkov <dp.w...@gmail.com> wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »