Best way to compare EXPECT result with our true records in Jasmine testing framework? - iphone

What are the ways to compare the expect results with our true records using Jasmine Testing Framework?
One of the way is to use a static values within expect Parameters which is good for very basic values... But it has several limitations like it does not compare objects at runtime...
How to compare objects at runtime for its validity...???
EDIT :
it("Read JSON record with Id.", function(){
result = Database.selectRecordById (STORE_ID, id3);
expect(result).toEqual(aRecord); //cValue
});
Here is the code. Now my problem is to compare the result value to aRecord. I will get result from method Database.selectRecordById. For now i am using a static value of aRecord. I want some other way which is more reliable so that my aRecord becomes dynamic. One thing i thought is to make a database which will contain all true values... but then i manually have to see that... What could be other alternative.??

As far as I see there is nothing wrong with static values for comparing result. That is how we usually do unit testing both in Java and in JavaScript.
The actual value is what you get from the actual database/method call and the expected value is a static value. The less 'moving parts' you have here, the better. If you were to dynamically load the expected values, that can go wrong as well and you do not want your tests failing if your application because your test data load was wrong. It is also a lot more cumbersome to maintain.
Hope I did not misunderstand your question.

Related

EXPECT unit testing function behaviour in KDB/q

I am working on writing a unit-test in kdb+/q and trying to understand the expect function which seems to take the format:
EXPECT[`function.name;1;ANY]
I could not find documentation on the function, or the function definition on the kdb/qunit github repo.
My assumptions, based on how the function looks were that the format covers:
EXPECT[`.function.name;return value; input params]
However, changing the return value to anything but 1 raises errors, and when running the tests, despite writing an expect line, the function seems to be called with the original input parameters and tries to perform the calculation, rather than returning 1.
I was wondering if someone could provide an example of the EXPECT should behave?

Is there another way way to replace paginate() with take()->get() where query param is present? (Laravel 9)

Usually I use paginate when I want the user to view a list (or a narrowed down list based on filters). Simple example below:
Thing::query()
->orderByDesc('created_at')
->paginate(40);
If I wanted the user to view a short list, like get the five newest models, I would create a separate api with a query like below:
Thing::query()
->orderByDesc('created_at')
->take(5)
->get();
I want to combine the two eloquent queries in such a way that it gets the paginated list by default, but will take 5 if the query param 'take=5' is present. I can do this the following way:
Thing::query()
->orderByDesc('created_at')
->when(
$request->query('take'),
fn ($query, $count) => $query->take((int)$count)->get(),
fn ($query) => $query->paginate(50)
);
The above works but has been described by a colleague as a little confusing, since the 3rd argument to when() is if the first argument is false (documentation) but that isn't immediately apparent when viewing the code. The "confusing" part might be subjective here but I would like to make sure my code is quickly understood by other devs as best as possible.
Does anyone know of a simpler/clearer or just another way to achieve this? In an ideal world the take()->get() would only exist in the when() method and paginate() would exist outside of it, but be overridden by the when() condition if true.
Note: I anticipate some people might say that they should remain as separate api's, however in my opinion the extra logic here is so simple that the gain in reduced code outweighs the gain in "do one thing well".

How do I "capture" and store the complex output of a Flutter function as code so I can write unit tests for it?

First of all, apologies if this is a stupid question. I'm new to unit tests so I'm struggling a bit here.
I'm working on an app that queries an API, receives a JSON response and then processes that response to produce a series of complex data structures. Many of these data structures are daily time series, which means each of my functions produces a list (List<Datapoint>) containing hundreds of datapoint objects.
What I'm trying to test is that, for a given API response, each function produces the output it should.
For the input of each test I have already grabbed a sample, real JSON response from the API, and I've stored it inside a test_data folder within my root test folder.
However, for the expect part... how can I obtain a sample output from my function and store it somewhere in my test_data folder?
It would be straightforward if the output of my function were a string, but in this case we're talking about a list with hundreds of custom objects containing different values inside them. The only way to create those objects is through the function itself.
I tried running the debugger to check the value of the output at runtime, which I can do... but that doesn't help me copy it or store it anywhere as code.
Should I try to print the full contents of the output to a string at runtime and store that string? I don't think this would work, as all I see in the console are a bunch of Instance Of when I do functionOutput.toString()... I would probably need to recursively print each of the variables inside those objects.
Please tell me I'm being stupid and there's a simpler way to do this :)

What is difference between LinkedHashSet and FilterSet(Guava)

When I trace legacy code, I found something very strange. I can get all the data values from LinkedHashSet, not from the other(FilterSet).
Although data can be seen in unfiltered, but in iteration, I cannot get the required data. Is it because of the predicate? How to get all the data from unfiltered ones?
I found that guava is used in the implementation of utilities class in my project.
What is the difference of these two sets? Any help is greatly appreciated.
Set<A<? extends B, ?>> attributes;
Sets.filter returns a private implementation type called FilteredSet that is a view of another set, restricting its output to only the elements of the original set that have some particular property.
There is deliberately no way to get out the original unfiltered data.

What's the best way to check that the output is stable using junit?

I have a method which takes a data structure and writes to an OutputStream. I'd like to write a unit test which ensures that the output of that function for a given input doesn't change unexpectedly.
I think I have the test structured the right way, using JUnit Theories. My #DataPoints are pairs of identifiers and example data structures, and I have an #Theory that the output from the method will be identical to a file checked in to version control that I fetch using getResourceAsStream using the identifier.
The painful part, however, is generating those files in the first place. My current very ugly solution is a boolean constant called WRITE_TEST_OUTPUT, which is usually false. When I want to regenerate the output files (either because I've changed the list of #DataPoints or the desired behaviour of the method has changed) I set this constant to true, and there's a test which when this is true runs the function again to write the current output files to a directory in /tmp. It then asserts that the constant is false, so I don't accidentally check in a version in which the constant is true.
It's very convenient to have the generation pretend to be a test so I can run it from within Eclipse, but it seems like a horrible kludge. This seems like the sort of thing people must do all the time - what kind of solutions have other people found?
What I'd probably do is detect the file's existence rather than use an externally modified constant. So you'd have something like
public void testMethodOutput() {
if (new File(expectedOutput).exists()) {
// check method output against file
} else {
// generate expected output from current behaviour; test passes automatically
// (saves output to the expected-output file)
}
}
When you want to update the expected output, delete the file, run tests, commit the new version to version control. No source change necessary.
It seems a little strange that the correct output is determined by looking at what a given version of the program does, rather than something you yourself set up (what if that version has a subtle bug?). Possibly that's a hint that your test is too coarse-grained, and that in turn might hint that your method is itself too coarse-grained. (Test smells are nearly always design smells.)