Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
    Siddhartha Gandhi
    @gandhis1
    Thanks. In my use case (mocking the protected method SendAsync of HttpMessageHandler) the HttpRequestMessage parameter can vary widely unless your testing "unit" is very small. So if a particular routine being tested does more than one request...GET/POST, different endpoints, different request content...I would need to match the argument. If I assert in the callback, won't that necessarily mean that only one of the N number of mocked calls will pass (and the rest would fail). Maybe this is just a byproduct of me trying to write things that are closer to integration tests than unit tests...but I think it would be useful to be able to supply custom IEqualityComparers for parameter matching
    Daniel Cazzulino
    @kzu
    well, yeah, that doesn't sound much of a unit test to me :smile:. you can simplify it all by just creating your own HttpRequestMessage-derived class that implements equality the way you want it though
    Siddhartha Gandhi
    @gandhis1
    I did try that, but it seems as though Equals() is called on the parameter actually passed in rather than the mock setup parameter. So instead of only needing to pass that derived class in Setup(), I would need to modify my actual code to use a HttpMessageHandler that uses the derived class behind the scenes. Anyway thanks for the help, for this particular use case I am actually pursuing using mockhttp instead, it's a little more forgiving here in that I can define a custom Func<bool> to match a request
    Kim Schmider
    @kim366
    Hello, I am trying to build Moq for debuggging. My first issue was that I needed to create IFluentInterface, as it is not in the project. Now it is complaining Process finished with exception The target "Restore" does not exist in the project. /repo/Directory.Build.props. What shall I do?
    Dominique Schuppli
    @stakx
    @kim366, sounds like something went wrong with NuGet package restore. (IFluentInterface is brought in via a NuGet package.) Are you using an up-to-date version of Visual Studio?
    Genco2
    @Genco2
    Hello, I am trying to mock the methods of UserPrincipal class for example FindByIdentity(). Is it possible with framework Moq? I am open for any resource advices since i am lost little bit. What shall I do ?
    Dominique Schuppli
    @stakx
    @Genco2, UserPrincipal.FindByIdentity is a static method. Moq cannot deal with those, nor with other non-virtual or sealed type members.
    What you can do is to define an interface for the same purpose, then create a main implementation that delegates to said method. Inject an instance of that interface in your application. In your test code, inject a mock for that interface instead.
    Lee Anderson
    @rgvlee
    Heya team, I'm writing an expression builder for Moq to allow easier verification of ILogger.Log invocations. Clearly it's not something that would ever be part of Moq, it is a helper which is scoped to Moq. With Moq.* reserved (I would presume) is there a preferred/standard convention for naming such community packages for something like this?
    Lee Anderson
    @rgvlee
    Beautiful, thanks very much
    Dominique Schuppli
    @stakx

    Heya @kzu, I've been meaning to ask you for a while whether you'd be OK with changing Moq 4's copyright notice so it includes contributors (such as myself :wink:).

    That's what we have right now:

    Copyright (c) 2007, Clarius Consulting, Manas Technology Solutions, InSTEDD.

    If it's OK with you, I'd like to change it to this:

    Copyright (c) 2007, Clarius Consulting, Manas Technology Solutions, InSTEDD, and Contributors.

    Daniel Cazzulino
    @kzu
    oh, yeah, of course!
    Dominique Schuppli
    @stakx
    Lovely.. thank you!
    churkinv
    @churkinv
    Hi everyone! Could anyone help ? I am testing .net core 3.1 app and have xunig and moq. I am trying to moq httpClient (trhought mocking HttpMessageHandler). And did it successfully, but partly, on second call of the method i got: Cannot access a disposed object.
    Object name: 'System.Net.Http.StringContent', I tried to SetupSequence on moq HttpMessageHandler, but still the same issue. So what is the best way to keep HttpClient alive on multiple calls? Just in case, out of test it works perfectly.
    churkinv
    @churkinv
    Sorry, guys, I found dummy error, I forget to remove some of "experiment" code in the tests method itself. So for the one who is struggling the solution above is working) Here is the adjusted working version:
    image.png
    Daniel Cazzulino
    @kzu
    @alevincenzi you can use VS2019 Community at home, it's free
    Matthew-Underwood
    @Matthew-Underwood
    Hello, this is probably not the right place for this discussion but I need someone who can point me in the right direction. Is there a general chat group or forums for unit testing and mocking in C#? I have a few questions regarding the design of the application in regards to mocking.
    IkeikeP
    @IkeikeP
    Hey, I've been trying to use Moq with IL2CPP backend (of Unity) and it's apparently not possible due to the nature of IL2CPP.
    Since then, I've found Moq version 5 which supposedly no longer uses "Reflection.Emit". Which could mean that using Moq compiled with IL2CPP would be possible.
    But after bumping Moq to version 5, I still see a similar issue where Reflection.Emit is used. Am I missing something?
    Daniel Cazzulino
    @kzu
    Yes, that's the goal. It's work in progress, though, with sporadic jolts of progress as I find time and energy to do it. With the advent of Roslyn-based SourceGenerators, I'm almost sure that that particular feature (compile-time proxy generation) will require the very latest VS that will ship with that)
    I currently have a feature branch that extends the current analyzer feature to do the same, but it doesn't perform very well with large solutions
    IkeikeP
    @IkeikeP
    Ohh. I was using moq4 which after finishing some tests, I realised it did not support IL2CPP, then I read the docs from moq5 and got hopeful to have it working. Unfortunately I will need to come up with a different solution that supports AOT. Regarding the VS requirement, I believe that might be lifted by adding the required microsoft dlls as dependency.
    Thanks a lot for your attention and reply. Keep up the amazing work.
    Daniel Cazzulino
    @kzu
    Yeah, the VS requirement wouldn't be necessary as long as the project references the right compiler toolset and roslyn packages, yep. I'm hoping to be able to complete that work by the time the feature ships to stable :smile:
    Oscar Colmenares
    @Graren
    Hello, I'm a web dev that just started on game dev. Trying to install Moq on unity running under ubuntu.
    So far no luck, downloading and pasting the release dir inside Plugins did nothing useful, can't install via Visual Studio and I ran out of ideas. How do I install Moq to use in ubuntu/unity? Thanks
    Daniel Cazzulino
    @kzu
    it's not a plugin, moq is a library and a nuget package. So i guess the same way you reference libraries in unity? (I haven't done any unity work at all)
    Oscar Colmenares
    @Graren
    As far as my short unity experience goes, adding something to the Assets directory inside unity makes the editor recompile and rebuild with the new package. Most packages have been inserted that way. However none of those packages had windows only code.
    I went as far as downloading the source and putting it inside my project, but there was some code that could not be processed in linux
    Michał Zegan
    @webczat
    Hi. any way to, in moq, do setup for two overloads of one method at once, or any overload, or something like this?
    Daniel Cazzulino
    @kzu
    Sure, everything you can do in plain c# manual mocks, you can do in moq
    Michał Zegan
    @webczat
    but what I wanted is, if possible, not to repeat the setup twice to achieve that. it's a xunit theory testing exceptions being thrown
    Daniel Cazzulino
    @kzu
    The compiler resolves the overload to call, so, you can't cheat it (easily at least).
    Michał Zegan
    @webczat
    currently both branches of the theory actually use only one overload, but that makes things tricky and I was researching...
    Shimmy
    @weitzhandler
    Hello everyone,
    I'm trying to verify a mock that involves access to an indexer (Microsoft.Extensions.Configuration.IConfiguration)
    For some reason, it's reported that the underlying SUT is accessing IConfiguration.Item when I call configurationMock.VerifyNoOtherCalls().
    Why is that, and how can I overcome this?
    Thanks
    Shimmy
    @weitzhandler
    Please mention my name when replying, so I get an email notification.
    Daniel Cazzulino
    @kzu
    best way to troubleshoot that is to create a strict mock, it will throw at the call-site that performs that call @weitzhandler
    Adam Houldsworth
    @RaringCoder

    Hi. I'm attempting to use It.Is<> outside of Setup but it seems to be returning null - I assume due to relying on knowing the current method. I was trying to add some code to remove boilerplate where the only thing changing is an argument (and condition expression) to a method call. Something like:

                var mockCommandBus = new Mock<ICommandBus>();
                mockCommandBus
                    .Setup(_ => _.PublishAsync(
                        testCommand,
                        It.IsAny<CancellationToken>()))
                    .ReturnsAsync(result);
                return (mockCommandBus.Object, mockCommandBus);

    Called:

    CommandBusBuilder.BuildMockCommandCall(
                    It.Is<RegisterNewApplication>(_ => _.Name == name && _.Description == description),
                    Result.Ok(new ApplicationId(id)));
    In my head it makes sense on paper, but evidently due to how the argument is resolved and expression evaluated, it sort of needs to be inside Setup. Anyone got any thoughts on this? Alternatives? I'm trying to avoid constantly writing the first block.
    Daniel Hughes
    @trampster
    Anyway to mock a method that takes or returns a Span<T>?
    Dominique Schuppli
    @stakx
    @RaringCoder With Moq 4, It.Is and other matchers only work when used inside LINQ expression tree. What's important is their identity, so to speak, and that is only preserved in a LINQ expression tree. Once evaluated, that is gone and all ypu're left with is their retuen value, which is in fact completely irrelevant & ignored by Moq.
    So if you want to avoid boilerplate you'd likely have to create a helper function (possibly parameterized) that creates similar setup/verify expressions for you as a whole. Extracting parts of an expression, then putting them back in somewhere else, means you have to go through the staticExpression.* factory methods which is going to be a pain in the b*tt compared to the C# compiler doing that for you automagically.
    @trampster, Moq 4 doesn't (and probably cannot) fully support Span<T> and other by-ref-like types due to limitations in .NET Reflection and LINQ expression trees. No way around it unfortunately.
    Daniel Hughes
    @trampster
    @stakx what about Moq 5?
    Dominique Schuppli
    @stakx
    @trampster I'm currently mostly involved with Moq 4, so I cannot say. @kzu should be able to tell you; or just give it a try. :wink:
    Daniel Cazzulino
    @kzu
    Haven't tried myself :(
    Daniel Hughes
    @trampster
    I took a quick look at Moq 5, but I struggled to find documentation/tutorials on how to use it
    There is a Docs folder, but it has very little in it and what is there is confusing and 2 years old.
    There also doesn't appear to be a nuget package for it
    Also there has only been one commit to it this year and that was in Feb
    Daniel Hughes
    @trampster
    What is the status of Moq 5 development? is it mostly complete, are there plans to release it in the near future?