Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
    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
    Dominique Schuppli
    @stakx
    @webczat This is definitely not possible with Moq 4.x and there's nothing we can do about that. It might be feasible with Moq 5.
    Worst case is that you'll need to resort to writing a mock class manually for types that use by-ref structs.
    Michał Zegan
    @webczat
    what would moq5 change in this respect?
    Dominique Schuppli
    @stakx
    Moq 5 has fewer limitations in general because it uses Roslyn to create mock types at compile-time, while Moq 4 generates mock types at runtime using System.Reflection.Emit. Generally speaking, Roslyn, being the default C# compiler, knows about all possible language constructs; in comparison, Reflection hasn't been brought fully up-to-date, and it cannot always deal with certain newer language constructs (such as by-ref structs). So Moq 4 inherits those limitations from Reflection, while Moq 5 is in principle much less constraints.
    That being said, I'm not overly familiar with Moq 5. It's entirely possible that it also cannot deal with by-ref structs at this time (I'd have to check) if its API is designed such that invocation Arguments get boxed as objects (boxing isn't allowed for by-ref structs, i.e. stack-only types). But as far as my current understanding of Moq 5 goes, that at least would be a limitation caused by its own API, and not by the underlying technology.
    Michał Zegan
    @webczat
    well but reflection.emit generates IL so it is not something that needs to be updated too much
    also probably linq expressions cannot represent ref structs, and params arrays use object boxing...
    Dominique Schuppli
    @stakx
    Reflection does more than just emit IL. Reflection presents you with higher-level abstractions like Type, MethodInfo etc. Try to do something with the much lower-level System.Reflection.Metadata or Mono.Cecil, and you'll soon see what System.Reflection provides beyond those. Like other parts in the BCL (say, LINQ expression trees, or CodeDOM), Reflection would ideally be updated together with C# language innovation... and that hasn't fully happened for quite a few years now. For example, try DynamicInvoke-ing a delegate that has spans in its signature; or try reflecting over all the custom modifiers that the C# compiler rmits these days. Yes, Reflection isn't the only limiting factor. You're already aware of some LINQ expression tree limitations (no assignment, no async/await, no default parameters, etc.) though Moq 4 can partly work around rhose. But at the end of the day, those parts of the BCL lag behind Roslyn.
    TL;DR: Moq 4 has some hard limits on what it can support. Moq 5 has limits too (though I don't know them precisely myself) but you at least have a chance there when it comes to spans.
    Michał Zegan
    @webczat
    aren't these modifiers actually attributes or so? as for dynamic invoking delegates how could you make such an api even at reflection side if you cannot box spans?
    also many of this is pretty logical, as in, reflection is not a c# specific api. it shouldn't technically get most of these... even some things it already got I'd say. it cannot recognize operators for example can it?
    as for code dom I think this api is ... hmm deprecated?
    Daniel Cazzulino
    @kzu
    hi there folks!
    What does everyone think about moving the community support over to Discord? moq/moq4#1089
    Dominique Schuppli
    @stakx
    My trigger finger merged your PR before I saw thid post. :-D But if you're more active on Discord than here, then moving makes good sense. :+1:
    If we do move for good, would there be any way of making this chat read-only / archived after adding a final redirection message?
    Daniel Cazzulino
    @kzu
    Good point. I'll investigate if that's possible
    Michał Zegan
    @webczat
    @stakx it seems like you are able to create expression tree for a lambda accepting a span when using factories on Expression instead of just letting the compiler construct it.
    Dominique Schuppli
    @stakx
    @webczat that's interesting, I didn't know that. But where to from from there? The underlying code generation & interception strategy still don't support spans. Plus manually constructing expression trees isn't user-friendly at all, that's not really a feasible approach for using Moq from the end-user perspective. ;-(
    Michał Zegan
    @webczat
    yeah. I discovered you can do that so started to think if one could make some kind of helpers or something, but there would likely be problems along the way, spans cannot be generic arguments, etc, so not sure if the rest of moq would happily work with them if I handcrafted such a tree
    not sure where spans are special and where they are not, as in, what except this would require special handling code. and whether you could or couldn't make helpers. but it could likely break in many suptle ways
    Dominique Schuppli
    @stakx
    I know I've mentioned it a few times already, but it's a crucisl point so I'll say it again: the code generation & interception strategy that Moq 4 uses, i.e. the underlying proxy generator (DynamicProxy) does not support spans. DynamicProxy takes all method arguments and puts them in a object[] array. This cannot be done for spans because they are stack-only types that cannot be boxed (i.e. converted to object). There is no way around this.
    Michał Zegan
    @webczat
    okay. we'd have to live with that. I doupt this can change for moq5 tbh
    Dominique Schuppli
    @stakx
    Why do you doubt that? Have you investigated it?
    Michał Zegan
    @webczat
    well. not enough to be sure. on the other hand restrictions on ref structs are very problematic, for example the fact they cannot become generic arguments. I believe you can generate any method you want in IL, on the other hand the possible problem is the appropriate ... user interface. not sure if moq5 is gonna be expression based or different, I just don't fully imagine how could that work.
    Dominique Schuppli
    @stakx
    I suggest you take a closer look before making too many assumptions.
    Michał Zegan
    @webczat
    still interested, will moq5 be still expression based?
    Dominique Schuppli
    @stakx
    Michał Zegan
    @webczat
    interesting
    Michał Zegan
    @webczat
    interesting how all that works... hope it's going to be released soon :D
    bradford roarr
    @broarr
    :wave: - I'm trying to test a library that only contains concrete classes. Is there a way I can use Moq to mock those concrete classes, or do I need an interface?
    Kevin B
    @Keboo
    you can use it with concrete classes, but you will only be able to mock the virtual methods on them.
    bradford roarr
    @broarr
    Awesome, thanks!