Sinon Stub/Spy Using WithArgs Not Behaving As Expected - sinon

When I specify withArgs for a sinon spy or stub, I expect the callCount to only count calls with those arguments. This doesn't seem to be happening, though.
If I run the following:
var mySpy = sinon.spy();
mySpy.withArgs("foo");
mySpy("bar");
expect(mySpy.callCount).to.be(0);
I get "expected 1 to equal 0". Am I crazy, is this a bug, or is there another way to do this?

You have to add withArgs to the assertion, too, like so:
var mySpy = sinon.spy();
mySpy.withArgs("foo");
mySpy("bar");
expect(mySpy.withArgs("foo").callCount).to.be(0);

Related

How to make an if statement point free

I have the following if statement:
var formatCity =
obj => R.both(has('city'), has('state'))(obj) ? appendCommaToCity(obj) : obj
I would like to make this code point free, but can not figure out a way around the if statement.
That's quite simple actually using the ifElse function - or its specialisation, when:
const formatCity = R.when(R.both(has('city'), has('state')), appendCommaToCity);

restubbing method in OCMock does not seem to work?

I am confused why this doesn't work...
[[[myObject stub] andReturnValue:#YES] isBadical];
NSLog(#"================> result: %i", [myObject isBadical]);
[[[myObject stub] andReturnValue:#NO] isBadical];
NSLog(#"================> new result: %i", [myObject isBadical]);
Result is:
2013-10-13 20:24:49.156 myApp[43197:c07] ================> result: 1
2013-10-13 20:24:49.157 myApp[43197:c07] ================> new result: 1
Is there a way to update the stubbed value without having to stop mocking and/or create a new mock object?
Use expect instead of stub. AFAIK it's not possible to stub a method twice with OCMock. You don't need to send verify after executing the code you want to test since you are not interested in verifying any expectations.

How do I create variable paths using e4X?

I need to know how I can parse a variable path in Flex 3 & e4X. For example, I have two XML strings where the name of one element is the only difference.
<NameOfRoot>
<NameOfChild1>
<data>1</data>
</NameOfChild1>
</NameOfRoot>
<NameOfRoot>
<NameOfChild2>
<data>2</data>
</NameOfChild2>
</NameOfRoot>
Currently I am accessing variables like this:
var data1:String = NameOfRoot.*::NameOfChild1.*::data;
var data2:String = NameOfRoot.*::NameOfChild2.*::data;
I would rather make this task more abstract so that if "NameOfChild3" is introduced I do not need to update the code. For example:
var data:String = NameOfRoot.*::{variable}.*::data;
Does anyone have insights into how this can be done?
Use the child property (LiveDocs example here):
var tagName:String = "NameOfChild1";
var data:String = NameOfRoot.child(tagName).data;
That's with no namespacing--not sure whether it's necessary in your case, but I assume you'd add some *::'s?
this also works:
var data:String = NameOfRoot..data;
but if you have more than 1 data node you'll have to sort some stuff out.
It looks like the ".*." operation will work. I wonder if this is the easiest way to handle this problem.
var data:String = NameOfRoot.*.*::data;

How can I tell if E4X expression has a match or not?

I am trying to access an XMLList item and convert it to am XML object.
I am using this expression:
masonicXML.item.(#style_number == styleNum)
For example if there is a match everything works fine but if there is not a match then I get an error when I try cast it as XML saying that it has to be well formed. So I need to make sure that the expression gets a match before I cast it as XML. I tried setting it to an XMLList variable and checking if it as a text() propertie like this:
var defaultItem:XMLList = DataModel.instance.masonicXML.item.(#style_number == styleNum);
if(defaultItem.text())
{
DataModel.instance.selectedItem = XML(defaultItem);
}
But it still give me an error if theres no match. It works fine if there is a match.
THANKS!
In my experience, the simplest way to check for results is to grab the 0th element of the list and see if it's null.
Here is your code sample with a few tweaks. Notice that I've changed the type of defaultItem from XMLList to XML, and I'm assigning it to the 0th element of the list.
var defaultItem:XML =
DataModel.instance.masonicXML.item.(#style_number == styleNum)[0];
if( defaultItem != null )
{
DataModel.instance.selectedItem = defaultItem;
}
OK I got it to work with this:
if(String(defaultItem.#style_number).length)
Matt's null check is a good solution. (Unless there is the possibility of having null items within an XMLList.. probably not, but I haven't verified this.)
You can also check for the length of the XMLList without casting it to a String:
if (defaultItem.#style_number.length() > 0)
The difference to String and Array is that with an XMLList, length() is a method instead of a property.

In Flash, how would I run an e4x statement when that statement is stored in a String?

So I have something like this:
var xmlStatement:String = "xmlObject.node[3].#thisValue";
What mystery function do I have to use so that I can execute xmlStatement and get thisValue from that xmlObject? Like....
var attribute:String = mysteryFunction(xmlStatement);
P.S. I know eval() works for actionscript2, I need the as3 solution. :)
Unfortunately this is not possible in ActionScript 3. This however might be a solution: http://blog.betabong.com/2008/09/23/e4x-string-parser/
For your example it would be:
var attribute : String = String( E4X.evaluate( XMLList(xmlobject) , 'node[3].#thisValue' ) );

Resources