Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
    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?
    Daniel Cazzulino
    @kzu
    The readme itself lists the nuget package feed:
    > CI package feed: http://kzu.nuget.cloud/index.json (CDN) or https://kzu.blob.core.windows.net/nuget/index.json (blob).
    Daniel Hughes
    @trampster
    OK I see, thats why I couldn't find it, it's not published to nuget.org
    Daniel Cazzulino
    @kzu
    no near future plans. I've been too busy :disappointed:. Also, for the proxy codegen, I plan on switching to roslyn source generators, coming in .net5 timeframe.
    Daniel Hughes
    @trampster
    I love the idea of using source generators
    Daniel Cazzulino
    @kzu
    yeah, in the repo currently, I have it based on AutoCodeFix and build-time generators
    but unfortunately that simply doesn't perform well for large API surfaces :disappointed:
    I tried optimizing it as much as possible using asp.net core but I don't think it's acceptable. So, source generators it is :smile:
    (well, will be, just need to find time... but I've been already playing quite a bit with those, so I got a good grasp of what it will take)
    bluechrism
    @bluechrism
    Hi all, sorry if this is not the right place for questions like this - please redirect me.
    I am updating a solution with around 10,000 tests from Moq 4.7.8 to Moq 4.14.5 and have most issues ironed (<50 failures I can attribute to the upgrade now) out but am stumped by odd behavior with Mock.As<T> where an object implements multiple interfaces and those interfaces define the same property. After setting the property, the object retains the old property value when casted to at least one of the interface versions and each time I correct the failing one, the previously successful one now fails. I am not sure that the changes in the changelog really describe the differences I am seeing.
    Is there someone who can help me understand what changed with Mock.As<T> , and help me understand the correct pattern for setting up my mock so the property behaves as intended?