chai equivalent of jasmine.any? - chai

I have an assert failing:
Assert: expect(obj1).to.deep.eq(expected);
It fails because obj1 has an updatedAt timestamp:
"updatedAt": 1510356196161
I want to be able to say on expected:
{
updatedAt: chai.any(number),
}
The equivalent of jasmine.any. Does this or a similar concept exist in chai?

Unfortunately, looks like the feature isn't implemented yet: https://github.com/chaijs/chai/issues/644#issuecomment-336197639
The best way to do such assertions that I've found so far is prop-by-prop:
expect(obj1).to.have.property('updatedAt').that.is.a('number');
expect(obj1).to.have.property('deletedAt').that.is.a('null');
expect(obj1).to.have.property('name').that.equals('John Doe');

Have you tried the following?
expect(obj1.updatedAt).to.be.a('number');

Related

Replace PHPUnit method `withConsecutive`

As method withConsecutive will be deleted in PHPUnit 10 (in 9.6 it's deprecated) I need to replace all of occurrences of this method to new code.
Try to find some solutions and didn't find any of reasonable solution.
For example, I have a code
$this->personServiceMock->expects($this->exactly(2))
->method('prepare')
->withConsecutive(
[$personFirst, $employeeFirst],
[$personSecond, $employeeSecond],
)
->willReturnOnConsecutiveCalls($personDTO, $personSecondDTO);
To which code should I replace withConsecutive ?
P.S. Documentation on official site still shows how use withConsecutive
I've just upgraded to PHPUnit 10 and faced the same issue. Here's the solution I came to:
$this->personServiceMock
->method('prepare')
->willReturnCallback(fn($person, $employee) =>
match([$person, $employee]) {
[$personFirst, $employeeFirst] => $personDTO,
[$personSecond, $employeeSecond] => $personSecondDTO
}
);
If the mocked method is passed something other than what's expected in the match block, PHP will throw a UnhandledMatchError.
Looks like there are not exists solution from the box.
So, what I found - several solutions
Use your own trait which implements method withConsecutive
Use prophecy or mockery for mocking.

Can we validate dynamically generated values like datetime or any other number in Karate DSL

Can we validate dynamically generated values like datetime or any other number in Karate DSL. If yes, could you please tell how do we do it ?
Just make a JavaScript function replicating that dynamic value. and then do karate matching.
* def datetime = function(){code_generating_Date_time}
Then match datetime == response.datetime
Although i feel like generating the function should not be done, because it may become non-deterministic
Would suggest redesigning the test case.
Yes.
For example if the response is { id: 'a9f7a56b-8d5c-455c-9d13-808461d17b91', name: 'Billie' }
You can assert this way:
{ id: '#string', name: 'Billie' }
Please read the documentation, because all of this is explained there: https://github.com/intuit/karate#fuzzy-matching

How do I assert to accept one of multiple possible values in JUnit

I have a test that returns some value after completion. The returned value is a String and can have one of several possible values based on some if condition in the test.
But in the assert statement I can check only one of the Expected values not both.
How can I do this? Thank you.
I will use AssertJ for this
assertThat(value).isIn(expected1, expected2, expected3);
The code is much simpler than with Hamcrest.
My solution was to save the result and then run a Jtest that the result was in an array of accepted solutions.
E.g:
`it("Test Message", () => {
const result = testFunction(inputValue);
assert.equal((result in [possibeResult1, possibleResult2, ...]), true);
});`

Query Appcelerator Cloud services Place with LIKE operator and case insensitive

Q) Is it possible to query Appcelerator cloud services Places objects (insensitive) where: - name LIKE 'fred' - SQL would be something like?
SELECT * FROM Places WHERE name like '%fred%'
I.e. query would return (if existing):
Fred
Alfred
Winnefred
Please tell me if this is possible with a simple code block using Ti.Cloud or REST or anything!
Note: I've read the documentation thoroughly but can't find an answer there. Please don't direct me to the documentation for the answer. Thanks.
Thanks.
Possibly. What you want is a $regex in the "where" parameter of Places.Query. The docs say it only supports prefix searches, but maybe it can do more. The below would be the equivalent of your select.
Ti.Cloud.Places.query({
where: {
name: { $regex: 'fred', $options: 'i' }
}
}, ...);
If it doesn't work, then you'll need to open up a feature request for it.
http://cloud.appcelerator.com/docs/api/v1/places/query
http://cloud.appcelerator.com/docs/search_query

How to access a stored value in PHPUnit_Extensions_SeleniumTestCase

How can I store a value within Selenium-RC (through PHPUnit) and then retrieve/access it later using PHPUnit?
Suppose I run a command like the following in a test:
$this->storeExpression( "foo", "bar" );
If I understand the Selenium API documentation correctly, I could access this data using javascript{storedVars['foo']} using good 'ol fashioned Selenese. It should contain the value "bar".
My question is this: how can I access this javascript{storedVars['test']} expression (or, more generally, javascript{storedVars} in PHPUnit?
For example, here's a simple test I've run:
public function testStorage()
{
$this->open('http://www.google.com/'); // for example
$this->storeExpression( 'foo', 'bar' );
$foo = $this->getExpression('foo');
echo $foo;
}
The output of which is "foo" (among the other standard PHPUnit output), while I expect it should be "bar". It's just giving me back the name of the expression, not its value.
Can anyone with experience in this give me some guidance?
Good posts in this thread, but looks like no 100% working answer so far.
Based on the Selenium reference here
http://release.seleniumhq.org/selenium-core/1.0/reference.html#storedVars
It would seem the correct code syntax would be:
$this->storeExpression( 'bar', 'foo' );
$foo = $this->getExpression("\${foo}");
I haven't tested that exactly, but doing something similar with
$this->storeHtmlSource('srcTxt');
$val = $this->getExpression('\${srcTxt}');
print $val;
did the trick for me.
The PHPUnit Selenium Testcase driver actually understands storeExpression and getExpression; have a look at its source code. You can do
$this->storeExpression('foo', 'bar');
and
$this->getExpression('foo');
As Selenium Stores the expression result in second argument it stores value in "bar" and when u need to call it you should call the stored name to get the expression.
$this->storeExpression( 'foo', 'bar' );
$foo = $this->getExpression("bar");
May this helps you it worked for me.
EDIT :
$evaluated = $this->getEval("regex:3+3");
$expressed = $this->getExpression("regex:3+3");
The First Evaluated will give the evaluated output for expression
and the second will show the expressed output.
The secound is used to verify that the specified expression is genrated or not by the alert.