General

I suggest you ...

You've used all your votes and won't be able to post a new idea, but you can still search and comment on existing ideas.

There are two ways to get more votes:

  • When an admin closes an idea you've voted on, you'll get your votes back from that idea.
  • You can remove your votes from an open idea you support.
  • To see ideas you have already voted on, select the "My feedback" filter and select "My open ideas".
(thinking…)

Enter your idea and we'll search to see if someone has already suggested it.

If a similar idea already exists, you can support and comment on it.

If it doesn't exist, you can post your idea so others can support it.

Enter your idea and we'll search to see if someone has already suggested it.

  1. SetupSequence() : More operators

    Be able to use SetupSequence like this : Example : The first time, I want the method returns 0, then it returns 1.

    mock.SetupSequence(x => x.GetNumber())
    .ReturnsFirst(0)
    .ThenReturns(1);

    Today, to do this I write this, it's a little bit verbose...

    var firstTime = true;

    mock.Setup(x => x.GetNumber())
    .Returns(()=>
    {
    if(!firstTime)
    return 1;

    firstTime = false;
    return 0;
    });

    src : http://stackoverflow.com/questions/11308328/moq-setupsequence

    I think it could be a good improvement to deal with sequences to add
    some function like these.

    25 votes
    Vote
    Sign in
    Check!
    (thinking…)
    Reset
    or sign in with
    • facebook
    • google
      Password icon
      I agree to the terms of service
      Signed in as (Sign out)
      You have left! (?) (thinking…)
      1 comment  ·  Admin →
    • please add a "Times" argument to IVerifies.Verifiable

      I would like to be able to specify the number of times a method should be called from a "Setup" so that if I need to setup multiple methods on a mock I can use a single Verify()

      For example, instead of:
      mock.Setup(x=>x.Foo()).Returns(1);
      mock.Setup(x=>x.Bar()).Returns(2);
      //exercise interface
      mock.Verify(x=>x.Foo(), Times.Once(), "Foo should be called once");
      mock.Verify(x=>x.Bar(), Times.Once(), "Bar should be called once");

      I would prefer:
      mock.Setup(x=>x.Foo()).Returns(1).Verifiable(Times.Once(), "Foo should be called once");
      mock.Setup(x=>x.Bar()).Returns(2).Verifiable(Times.Once(), "Bar should be called once");
      //exercise interface
      mock.Verify();

      Thank you for your consideration.

      20 votes
      Vote
      Sign in
      Check!
      (thinking…)
      Reset
      or sign in with
      • facebook
      • google
        Password icon
        I agree to the terms of service
        Signed in as (Sign out)
        You have left! (?) (thinking…)
      • Provide a callback before returning default mocks as values

        When you set up a Mock with DefaultValue.Mock it will return mock instances for any members that don't have Setups defined.

        I'm doing a lot of work with AutoMocking containers, and in that scenario it would be really nice to have a callback that would allow me to intercept this behavior.

        My AutoMocking container will take care of returning a mock of IFoo, and with DefaultValue.Mock, a member will automatically return a mock of IBar.

        However, I would really like to be able to say that "whenever Moq tries to return IBaz from an IBar method, I want to be…

        15 votes
        Vote
        Sign in
        Check!
        (thinking…)
        Reset
        or sign in with
        • facebook
        • google
          Password icon
          I agree to the terms of service
          Signed in as (Sign out)
          You have left! (?) (thinking…)
          planned  ·  kzuAdminkzu (Admin, moq) responded

          yes, we had a request to allow the default value strategy be open (it’s already pluggable internally)

        • Make it possible to verify the number of times a protected member was invoked

          It would be nice if protected setups could be verified using Times (Never, Once, etc.) just like normal setups.

          13 votes
          Vote
          Sign in
          Check!
          (thinking…)
          Reset
          or sign in with
          • facebook
          • google
            Password icon
            I agree to the terms of service
            Signed in as (Sign out)
            You have left! (?) (thinking…)
          • Matching parameters which are expressions

            Maybe it is already somehow possible, but I need to be able to do something like this:

            mock.Verify(x => x.Foo(y => Bar(It.Is<int>(z => z == 2))), Times.Once);

            Verify must match calls to Foo where the parameter is an expression calling 'Bar' with a parameter which must equal '2'.

            I know I can write the following, which however is not quite specific enough.

            mock.Verify(x => x.Foo(It.IsAny<Expression<Func<int, int>>()), Times.Once);

            10 votes
            Vote
            Sign in
            Check!
            (thinking…)
            Reset
            or sign in with
            • facebook
            • google
              Password icon
              I agree to the terms of service
              Signed in as (Sign out)
              You have left! (?) (thinking…)
              1 comment  ·  Admin →
            • Make callbacks more flexible about the required parameters

              Sometimes you don't need all the parameters of the mocked method in the callback. Sometimes you don't need any of them.

              Is it possible to allow partial matches, I'm thinking if there are 4 parameters on the method, and only 3 on the callback, if the first 3 match then allow that callback.

              As the callback verification seems to be done at run time I think this should be possible.

              8 votes
              Vote
              Sign in
              Check!
              (thinking…)
              Reset
              or sign in with
              • facebook
              • google
                Password icon
                I agree to the terms of service
                Signed in as (Sign out)
                You have left! (?) (thinking…)
                1 comment  ·  Admin →

                what should be the batching behavior? Argument type obviously, but what about ordering? What if the method has 2 string parameters mixed with other types?

              • Add VerifyNoMoreIntractions and VerifyZeroInteractions

                This will allow to write tests like the following, which AFAICT are not possible at the moment:

                // Arrange:
                var mock = new Mock<IFoo>();
                // Automatically return defaults for everything, so no additional setup.

                // Act:
                ...

                // Assert:
                mock.Verify(foo => foo.SomeMethod());
                mock.Verify(foo => foo.AnotherMethod());
                mock.VerifyNoMoreInteractions();

                Of course, one could use strict mode and specify all the expectations in advance, but that somewhat breaks the AAA approach.

                6 votes
                Vote
                Sign in
                Check!
                (thinking…)
                Reset
                or sign in with
                • facebook
                • google
                  Password icon
                  I agree to the terms of service
                  Signed in as (Sign out)
                  You have left! (?) (thinking…)
                • Add reporting to to mocks, outputing as much information as possible for every method invoked

                  When your debugging or simply trying to understand what is happening it would be very useful if we could get a list of all the methods that were invoked on a particular mock (or all mocks from a factory)

                  This could either be in the form of an explicit call:

                  mock.Report();

                  or as part of the ToString():

                  Console.Writeline(mock.ToString());

                  6 votes
                  Vote
                  Sign in
                  Check!
                  (thinking…)
                  Reset
                  or sign in with
                  • facebook
                  • google
                    Password icon
                    I agree to the terms of service
                    Signed in as (Sign out)
                    You have left! (?) (thinking…)
                    1 comment  ·  Admin →

                    we have significantly improved this reporting in case of verify errors. Would you want this to also be available when the verifications actually succeed too?

                  • Publish Debug Symbols (pdb)

                    Please provide the pdb for recent build via SymbolSource.org at https://www.symbolsource.org/Public/Metadata/Default/Project/Moq

                    or build the project on free for open source project CI server such as http://www.appveyor.com, https://drone.io, https://travis-ci.com, etc.

                    6 votes
                    Vote
                    Sign in
                    Check!
                    (thinking…)
                    Reset
                    or sign in with
                    • facebook
                    • google
                      Password icon
                      I agree to the terms of service
                      Signed in as (Sign out)
                      You have left! (?) (thinking…)
                    • Make DynamicProxy layer interchangeable

                      I implemented my own DynamicProxy and would like to put my implementation underneath the Mock<> facade.

                      4 votes
                      Vote
                      Sign in
                      Check!
                      (thinking…)
                      Reset
                      or sign in with
                      • facebook
                      • google
                        Password icon
                        I agree to the terms of service
                        Signed in as (Sign out)
                        You have left! (?) (thinking…)
                      • Support for auto-mocking hierarchies (recursive mocks)

                        a thought. the need for auto-mocking hierarchies (recursive mocks) is a smell that LoD has been violated.

                        is good to support auto-mocking hierarchies then ?

                        4 votes
                        Vote
                        Sign in
                        Check!
                        (thinking…)
                        Reset
                        or sign in with
                        • facebook
                        • google
                          Password icon
                          I agree to the terms of service
                          Signed in as (Sign out)
                          You have left! (?) (thinking…)
                        • Introduce a MoqException to wrap fex. ArgumentException

                          I had a Unit Test with following attribute: [ExpectedException(typeof(ArgumentException))]
                          So the test would succeed when this exception was thrown.

                          But because I was incorrectly using Moq, my test would always pass. During the Setup() method, Moq was throwing an ArgumentException. Now if incorrect usage (fex using a sealed class to mock, or a property that's not defined as virtual) would throw MoqException, the test would fail, causing me to search for the problem.

                          4 votes
                          Vote
                          Sign in
                          Check!
                          (thinking…)
                          Reset
                          or sign in with
                          • facebook
                          • google
                            Password icon
                            I agree to the terms of service
                            Signed in as (Sign out)
                            You have left! (?) (thinking…)
                            1 comment  ·  Admin →
                          • 3 votes
                            Vote
                            Sign in
                            Check!
                            (thinking…)
                            Reset
                            or sign in with
                            • facebook
                            • google
                              Password icon
                              I agree to the terms of service
                              Signed in as (Sign out)
                              You have left! (?) (thinking…)
                            • Ability to disable saving references to arguments the mock was invoked with

                              At my company I'm using Mock to unit test for memory leaks.

                              Our UI controls typically take some interface of a model, binds to its events and then updates the UI state depending on this model. When the UI gets disposed it's crucial that it unhooks completely from the model and can be garbage collected.

                              I unit test this by
                              1. Making a mock of the model interface
                              2. Use the mock to create the control
                              3. Attach a weak reference to the control
                              4. Dispose the control
                              5. Run a garbage collection
                              6. Check if the weak reference is…

                              3 votes
                              Vote
                              Sign in
                              Check!
                              (thinking…)
                              Reset
                              or sign in with
                              • facebook
                              • google
                                Password icon
                                I agree to the terms of service
                                Signed in as (Sign out)
                                You have left! (?) (thinking…)
                              • Make the internal recording of actual invocations optional in order to reduce memory usage by Moq

                                I use Moq simply to produce stubbed interfaces. I have no need to check the invocations of each method for some of my mocked interfaces. In some cases, the number of invocations is in the 100,000's, which causes many OutOfMemoryExceptions to be thrown unexpectedly.

                                Being able to turn off invocation tracking for specific mocks would fix this issue.

                                3 votes
                                Vote
                                Sign in
                                Check!
                                (thinking…)
                                Reset
                                or sign in with
                                • facebook
                                • google
                                  Password icon
                                  I agree to the terms of service
                                  Signed in as (Sign out)
                                  You have left! (?) (thinking…)
                                • Can't raise event on recursively-mocked interface

                                  Raise() doesn't raise an event on a recursively-mocked interface (i.e. the event belongs to the interface of a recursively-mocked property).

                                  3 votes
                                  Vote
                                  Sign in
                                  Check!
                                  (thinking…)
                                  Reset
                                  or sign in with
                                  • facebook
                                  • google
                                    Password icon
                                    I agree to the terms of service
                                    Signed in as (Sign out)
                                    You have left! (?) (thinking…)
                                    1 comment  ·  Admin →
                                  • improve the errormessage for verifying Times.Once, when method is not called

                                    mock.Verify(m => m.Do(), Times.Once);

                                    The errormessage says the method was invoked more than once, even though the method was never invoked.

                                    3 votes
                                    Vote
                                    Sign in
                                    Check!
                                    (thinking…)
                                    Reset
                                    or sign in with
                                    • facebook
                                    • google
                                      Password icon
                                      I agree to the terms of service
                                      Signed in as (Sign out)
                                      You have left! (?) (thinking…)
                                      1 comment  ·  Admin →
                                    • Fix the several broken links on the moq home page

                                      There are several broken links on the home page, which gives the impression this project is either dead or dying. Is it? (http://code.google.com/p/moq )

                                      2 votes
                                      Vote
                                      Sign in
                                      Check!
                                      (thinking…)
                                      Reset
                                      or sign in with
                                      • facebook
                                      • google
                                        Password icon
                                        I agree to the terms of service
                                        Signed in as (Sign out)
                                        You have left! (?) (thinking…)
                                      • Take into account existing tests before removing things

                                        The Isuses log shows items that are going to be removed for release 3.5. While Rhino Mocks takes backwards compatibility a bit too far there is something to be said for stability in code that tests rely on. If every release or two of Moq I have to refactor a lot of tests there is no way I'm going to be able to continue using it.

                                        2 votes
                                        Vote
                                        Sign in
                                        Check!
                                        (thinking…)
                                        Reset
                                        or sign in with
                                        • facebook
                                        • google
                                          Password icon
                                          I agree to the terms of service
                                          Signed in as (Sign out)
                                          You have left! (?) (thinking…)
                                          1 comment  ·  Admin →

                                          We’re working hard to keep backs compat, by basically hiding old/legacy API members and keeping compilation intact.

                                          We do remove members after 2 major versions, so you have to refactor as needed. That said, the intermediate version (the one containing the obsolete and warnings) helps you to see what you should be using instead.

                                          At the end of the day, if a given version of Moq does everything you need, you have no reason to upgrade to an incompatible version (which wouldn’t happen more than once a year, approx). I just pushed a couple breaking changes to 4.0 as that’s the next major release post-3.0.

                                          Keeping legacy APIs for 2 major versions gives you plenty of time to refactor or consider NOT to upgrade. We want to support new Moq scenarios without confusing new users with legacy APIs that are no longer in use.

                                          Would you prefer that we keep…

                                        • Expose IDefaultValueProvider?

                                          For example, if I want to override a loose mock to also return empty lists or a dummy object for a specific type.

                                          Currently, I've set up a mock factory class that will use reflection on the interface after the mock has been created. If a method returns a type that I'd like a different default value for, it uses reflection to call Setup using that method and returning the empty list / dummy object. It works, but it's really slow.

                                          2 votes
                                          Vote
                                          Sign in
                                          Check!
                                          (thinking…)
                                          Reset
                                          or sign in with
                                          • facebook
                                          • google
                                            Password icon
                                            I agree to the terms of service
                                            Signed in as (Sign out)
                                            You have left! (?) (thinking…)

                                            It also returns empty for enumerables and iqueryables.
                                            if you set the default value to DefaultValue.Mock, you’d get an empty list too. Have you tried that?

                                          ← Previous 1
                                          • Don't see your idea?

                                          General

                                          Feedback and Knowledge Base