Moq Second setup overrides the value returned for the first stetup - moq

I have a setup for :
_fusionPORepMock.Setup(s => s.GetByFusionVersionID(It.Is<int>(i => i.Equals(123)))).Returns(_currentPO);
_fusionPORepMock.Setup(s => s.GetByFusionVersionID(It.Is<int>(i => i.Equals(111)))).Returns(_previousPO);
However Moq overrides the value for the first setup even if the arguments are different.
Any ideas or concrete solution for my problem?
I do not want to create another mock.

I'm not completely sure why using It.Is in this way clobbers the first setup, but if you just pass the raw integer you'll get the behavior you'll expect (and with less code):
_fusionPORepMock.Setup(s => s.GetByFusionVersionID(123)).Returns(_currentPO);
_fusionPORepMock.Setup(s => s.GetByFusionVersionID(111)).Returns(_previousPO);

Related

Moq: Proper way to verify that a method was called just once with given parameters?

To ensure that a method got executed just once with given parameters (and only with those parameters), I think I have to check it twice so like:
_fileHandlerMock.Verify(x => x.DeleteFile("file.txt"), Times.Once);
_fileHandlerMock.Verify(x => x.DeleteFile(It.IsAny<string>()), Times.Once);
Is there a better way to check, something like an "exclusive" option or so?
Moq library provides the method specifically for that purpose. It is VerifyNoOtherCalls, it is used in combination with the verifying and it will ensure that no other calls have been made except the (already) verified calls.
_fileHandlerMock.Verify(x => x.DeleteFile("file.txt"), Times.Once);
_fileHandlerMock.VerifyNoOtherCalls();

Result functions of type x => x are unnecessary?

There might be a gap in my understanding of how Reselect works.
If I understand it correctly the code beneath:
const getTemplates = (state) => state.templates.templates;
export const getTemplatesSelector = createSelector(
[getTemplates],
templates => templates
);
could just as well (or better), without loosing anything, be written as:
export const getTemplatesSelector = (state) => state.templates.templates;
The reason for this, if I understand it correctly, is that Reselect checks it's first argument and if it receives the exact same object as before it returns a cached output. It does not check for value equality.
Reselect will only run templates => templates when getTemplates returns a new object, that is when state.templates.templates references a new object.
The input in this case will be exactly the same as the input so no caching functionality is gained by using Reselect.
One can only gain performance from Reselect's caching-functionality when the function (in this case templates => templates) itself returns a new object, for example via .filter or .map or something similar. In this case though the object returned is the same, no changes are made and thus we gain nothing by Reselect's memoization.
Is there anything wrong with what I have written?
I mainly want to make sure that I correctly understand how Reselect works.
-- Edits --
I forgot to mention that what I wrote assumes that the object state.templates.templates immutably that is without mutation.
Yes, in your case reselect won't bring any benefit, since getTemplates is evaluated on each call.
The 2 most important scenarios where reselect shines are:
stabilizing the output of a selector when result function returns a new object (which you mentioned)
improving selector's performance when result function is computationally expensive

Passing Variables from context to another context

so i need to use a variable from a context that a caller called to the context that the called party uses, so i have a code like this
[calledContext]
exten => s,1,goto(waits)
same => n,goto(playmessage)
same => n(waits),set(a=0)
same => n(waits2),wait(5)
same => n,GotoIf($[${a} = 0]?waits2:hang)
same => n(playmessage),noop(${randomId})
same => n(hang),hangup();
[callerContext]
exten => 012345,1,Noop()
same => n,set(randomId=523)
same => n,Dial(SIP/09201234567,20,G(calledContext^s^1)g)
same => n,hangup()
heres my problem, in the calledContext context, when i use noop() on ${randomId}, nothing is displayed, how do i pass the value of the randomId from callerContext to calledContext?
SHARED is not what you want here.
The G option in Dial is one of those fun options that sends the two channels involved in the Dial operation to different places. Both are going to be sent to the Context/Extension you specified, which - given the names of the contexts - is probably not what you want. The called party gets sent to priority+1; the calling party priority.
That is, I'd expect the called party to start executing at calledContext,s,2 and the calling party to start executing at calledContext,s,1. But that's ancillary to your issue.
A variable set on an inbound channel can be passed to the channels it dials using channel variable inheritance. That is:
exten => 012345,1,Noop()
; Using a single underbar will set randomId on all channels this
; channel creates, but will not continue to propagate further
same => n,set(_randomId=523)
; Using two underbars will cause the variable to always propagate
same => n,set(__someOtherId=111)
same => n,Dial(SIP/09201234567,20,G(calledContext^s^1)g)
same => n,hangup()
Using the _ or __ will cause the variables to be set on the called party. If you want the variables to continue to propagate to channels the called party may also call, then use __.
Asterisk use concepts of CHANNEL or leg.
So in your call you have 2 channel, each one have independent variables and control structure
You can see name of bridged channels by BRIDGEPEER
http://www.voip-info.org/wiki/view/Asterisk+Detailed+Variable+List
You can get variables of other channel by using function SHARED maybe.
http://www.voip-info.org/wiki/view/Asterisk+func+shared
For see variables set in current channel you can use application DumpChan

Newbie trying to use Moq for enumerable method

I'm trying to see if Moq is something I'd like to use in a new project as the other mocking frameworks I've used are challenging IMHO. So for instance, I have a method as such:
IEnumerable<PickList> GetPickLists();
I'm not sure how I'm supposed to mock this... I've tried something like this, but I'm getting compliation errors (I know the following
Returns() isn't correct, but can't figure out what to put in the Returns body:
var mockCrm = new Mock<ICrmProvider>();
mockCrm.Setup<IEnumerable<PickList>>(foo => foo.GetPickLists())
.Returns<IEnumerable<PickList>>({});
Also, trying to mock something like these two methods:
CustomerSyncResult ApplyActions(IEnumerable<CustomerAction> actions);
IEnumerable<Customer> GetCustomers(IEnumerable<string> crmIDs, IEnumerable<string> emails);
I know I'm asking a blanket question, but I'm having a heck of a time getting started. The CHM in the download doesn't have enough samples for me and some of the tutorials out there seem to be using obsolete methods as well as not covering enumerations which makes it tricky for me :(
Any tips would be greatly appreciated.
Try
mockCrm.Setup(x => x.GetPickLists())
.Returns(new List<PickList>());
The QuickStart is a good reference.
Some examples for the other methods:
mockCrm.Setup(x => x.ApplyActions(It.IsAny<IEnumerable>()))
.Returns(new CustomerSyncResult());
mockCrm.Setup(x => x.GetCustomers(It.IsAny<IEnumerable>(),
It.IsAny<IEnumerable>()))
.Returns(new List<Customers>());
As an aside, make the IEnumerable generic in your original interface for better type safety.
You can also use the new Moq v4 functional specifications:
var list = new List<PickList> { new PickList() };
ICrmProvider crm =
Mock.Of<ICrmProvider>(
x =>
x.GetPickLists() == list);
That is not as well documented currently. Note that you no longer have to write mock.Object. Some links:
Old style imperative mocks vs moq functional specifications
Moq Discussions: Mock.Of - how to specify behavior?
The exact syntax (using It.Is, the contents of the lists, etc.) will depend on what you're trying to accomplish. It.IsAny will match any argument, which will make things easier when dealing with sequence or collection parameters.

Getting Dictionary<K,V> from ConcurrentDictionary<K,V> in .NET 4.0

I'm parallelizing some back-end code and trying not to break interfaces. We have several methods that return Dictionary and internally, I'm using ConcurrentDictionary to perform Parallel operations on.
What's the best way to return Dictionary from these?
This feels almost too simple:
return myConcurrentDictionary.ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
I feel like I'm missing something.
Constructing the Dictionary<K,V> directly will be slightly more efficient than calling ToDictionary. The constructor will pre-allocate the target dictionary to the correct size and won't need to resize on-the-fly as it goes along.
return new Dictionary<K,V>(myConcurrentDictionary);
If your ConcurrentDictionary<K,V> uses a custom IEqualityComparer<K> then you'll probably want to pass that into the constructor too.
Nope. This is completely fine. .NET sequences are just nice like that. :D

Resources