❔ ✅ Verifying the purpose of Moq usage
So, as you can see I use
It.IsAny<int>()
to mimic a value that would be passed, since GetUserName()
requres an integer passed as argument.
However this value is not used in the actual mock, but rather the
the 42
that is passed here, which then calls GetUserName()
inside of it.35 Replies
Now this makes sense to me, but I just need to be sure that the purpose of
It.IsAny<int>()
here is to comply with the fact that GetUserName()
requires one argument, and therefore just writing it without any value would not compile.
Is what it Assert
's on, which is the 42
, not the random value or the default(T)
I think It.IsAny<int>()
would return.not the random value or the default(T) I think It.IsAny<int>() would return.thats not at all what
It.IsAny
does
it doesnt actually make a value, its telling Moq that "regardless of what int is passed in"
it just has to be an intmore like a constraint then?
you could do
if this setup should only trigger on that value
Ahh, so only when called with
42
it will workyeah
string result = userManager.GetUserDisplayName(42);
would work, but if I change it to 32
it will throw an exception?this might be useful if you have a test where the thing you test calls the same dependant method twice
but with different expected return values
so you basically ensure that the value used is the same
otherwise the test is obviously rendered useless
Makes sense, what if the condition of
42
(in this example) isnt met? Exception I assume, ordefault
iirc
so 0 for an int
you can however use moqs verification tools to make sure only some calls were madeI see, I dont think I need to get that deep yet. My work is slowly implementing Moq, so I guess I'll learn about it when I need to use something like that
fancy though
default
for a string is nullso if the condition isnt met, it returns
null
so yeah?
btw, a lot of people are moving away from Moq currently
Oh, really? I was under the impression that this is the thing to use
same with Xunit
oh boy
thats not a small accusation actually
tldr: the author added a weird obfuscated dll that he was also the author of, that slowed down CI builds and dumped messages in the logs about donating to projects, and it gathered git author emails while doing so
SponsorLink DLLs contain obfuscated code
people were rightly pissed as hell
he did revert the changes after a few days, but its clear he still believes he did nothing wrong
he obfuscated it too, thats tricky
so a lot of people just lost trust in him
Yeah, makes sense. Dangers of updating your packages without looking at its changes I suppose
kind of sad that we have to be careful with things like that
it is indeed.
anyways, a lot of people are thus moving away from Moq and using NSubstitute instead
its similar in terms of features, just has a different syntax
you can ofc keep using Moq, just thought you should know
Alright, thanks for the heads up. I will discuss this with my teamleader, see whats up
As for the
It.Any<int>()
, in the case of my example. Would you consider doing:
better practice than It.Any<>
?nah
when it comes to mocking, just keep it simple and readable
in that case, it doesnt matter
but I'd be wary of hardcoding stuff like the
42
regardless, even in the assert
id rather use constants
so if you have to change it for some reason, its changed everywhereyes I'd write it like this:
speaking of
sure
how do u declare a constant in C#
const
, final
?const int userId
is that a runtime constant
or a compile time one
no
compile time
Alright, sweet nice
Thanks, I'll stop bothering you now :)
/close
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.