Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
    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!
    julian-code
    @julian-code

    Hey moq creators! Nice library. I really like it, and use it daily.

    I have alot of discussions about strict mocking vs. loose mocking. I know Loose mocking is the default. I have tried to find out why that is, but I cant seem to find anything about it. Can you explain why that is?

    I do prefer loose mocking, and just stubbing the relevant methods for my Assert. When is Strict preffered?

    Thumann
    @Thumann

    Hi, I'm trying to raise the following event but i'm getting a Parater count mismatch exception, any ideas?
    event Func<ProcessMessageEventArgs, Task> ProcessMessageAsync;

    processorMock.Raise(c => c.ProcessMessageAsync += null, new ProcessMessageEventArgs(msgMock.Object, receiverMock.Object, CancellationToken.None));

    Dominique Schuppli
    @stakx
    Without looking at this in detail, I'll hazard a guess. .NET events and async/Task don't mix. I suggest you convert your event definition to event Action<ProcessMessageEventArgs> and your handlers to [async] void methods.
    I cannot currently check, but IIRC there are different overloads of Raise. Some require an object sender argument, some don't. Make sure you're using the overload that doesn't want a sender.
    Alp
    @aysan0

    image.png

    going back to this post, I believe I have a similar issue. I have a mock that explicitly uses SetupAllProperties. Here is an example:
    (note, this mock doesn't have strict behavior, but I can add it. It is mocking myClass)

    this.myMock.SetupAllProperties()
    this.myMock.SetupGet( p => p.propertyA).Returns(<some_value>);

    Later, in the code (not in the test), there is a set on the property as following:

    myClass.propertyA = some_method_to_get_a(input1, input2, etc..);
    
    //and in another method
    
    var someTaskClass = new SomeTaskClass(myClass.propertyA);
    
    public SomeTaskClass(propertyA)
    {
          this.myProperty = propertyA
    }

    In the test code, I have an assert statement as follows:

    Assert.AreEqual(expectedA, this.SomeTaskClass.myProperty, "message here");

    In Moq 4.10.1, this worked and I believe it is due to the SetupGet method being called. I no longer have this working in version 4.15.2. I am very very new to Moq. Is there any suggestions on this?

    asked the similar question here: moq/moq4#1066 prior to finding this channel :)
    Alp
    @aysan0
    ^ ping on this in case anybody knows
    Peter G Jones
    @Jonesie

    Hi. Having problems with setup on my cache method. Im sure it's something simple but my brain is jelly.

    Task<T> GetAsync<T>(string key, Func<Task<T>> fetchValue);

    Ive got this:

    _cacheMock.Setup(m => m.GetAsync<Shard>(It.IsAny<string>(), It.IsAny<Func<Task<Shard>>>())).Returns<Func<Task<Shard>>>(async f => await f());

    But I get an error when I run the tests:

    System.ArgumentException : Invalid callback. Setup on method with 2 parameter(s) cannot invoke callback with different number of parameters (1).
    Peter G Jones
    @Jonesie
    Doh. Asking the question is sometimes enough to figure out the answer.
    _cacheMock.Setup(m => m.GetAsync<Shard>(It.IsAny<string>(), It.IsAny<Func<Task<Shard>>>())).Returns<string, Func<Task<Shard>>>(async (s,f) => await f());