Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
    Oscar Colmenares
    @Graren
    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?
    Dominique Schuppli
    @stakx
    @bluechrism it might help if we had a minimal code example to look at. Can you e.g. boil down one of your failing tests to just two or three interfaces with only one property each, then state your expectatipns (probably whatever would've happened with version 4.7), what happened instead (using the latest version), and why you think that isn't right? If so please post an issue over at https://github.com/moq/moq4.
    bluechrism
    @bluechrism
    OK, I'll work out a minimal set and can post it up as an issue.
    bluechrism
    @bluechrism
    image.png
    Hey, before i write up another issue (and a full repro is coming for the other one), can someone explain this difference between new Mock<T> and using Mock.Of<T> with Mock.Get to set up a property: Again, both the Tests would pass with Mock 4.7.8 but the second now fails with 4.14.5.
    Nikola Radin
    @lepijohnny
    @bluechrism Looking at the code base 4.14.5 used to setup the mock using the method SetupAllProperties which effectively setup stubs. That means that you would have a real get\set for all the properties. You can avoid that by using MockBehavior.Strict. The only question is why these tests are passing on 4.7.8, seems they should fail there as well...
    bluechrism
    @bluechrism
    @lepijohnny is you comment about SetupAllProperties referencing Mock.Of<T> then?
    Nikola Radin
    @lepijohnny
    @bluechrism Ah, sorry, my post is indeed confusing :). Mock.Of<T> uses SetupAllProperties(if not MockBehavior.Strict) under the hood. This means all properties will be stubbed. This is the reason why 2nd example fails.
    bluechrism
    @bluechrism
    @lepijohnny Thanks for the clarification.
    Shimmy
    @weitzhandler
    image.png
    image.png
    How do I build moq4? I'm getting the errors above
    Kevin B
    @Keboo
    Hi all, I am one of the maintainers on Moq.AutoMocker. I am wondering if it would be ok to use Moq logo as the icon for the NuGet package. Specifically this one from the org page. I also noticed that the Moq NuGet package does not appear to have a icon set. I would be happy to submit PRs on both projects if that is acceptable.
    Dominique Schuppli
    @stakx
    Hey @Keboo - IMO a PR for the package icon would be great! :+1:Regarding your first question (using the icon for your package) my suggestion would be to use a slightly modified icon for third-party packages... but this is ultimately @kzu's decision.
    ajmcateer
    @ajmcateer
    Hey with Setup Sequence I have specified 4 return values, If I call a 5th time it returns null, is there anyway to make it return the last value forever?
    Dominique Schuppli
    @stakx
    @ajmcateer - no, that is currently not possible. You'd need to be able to do something along the lines of sequenceSetup.ReturnsRange(Enumerable.Repeat(lastValue, int.MaxValue)) though that hasn't been implemented. We've briefly discussed this in moq/moq4#573.
    Depending on the situation you may be able to work around that by taking advantage of the fact that once exhausted, sequence setups fall back to producing default values. So you could e.g. set up the desired default value using mock.SetReturnsDefault. (Though take note that this is not specific to one method setup, but to all of a mock's methods.)
    Dominique Schuppli
    @stakx
    @bluechrism, Gitter only just showed me your above post from Sept 3. That's actually a regression (IMO), see moq/moq4#1066.
    Michał Zegan
    @webczat
    question: there is no way to mock methods or verify methods that take spans as parameters? I may really need that. fortunately most tests related to these would just test for return value correctness, but