Shouldn't "toHaveBeenNthCalledWith" test only one instance of the mocked function - node.js

From my understanding, toHaveBeenNthCalledWith within a jest test should test only the "Nth" call with "N" being a non-zero number. My test below returns the Nth call and the one after it. In the test, I give N a value of one and it is showing two. Now I can replaced the one with a two and then it will show 2 and 3. This is causing my test to fail. Are there any suggestions to why it is behaving like this??

Probably your mock is saving state between tests, this means that it "holds" previous calls, therefore, you see 4 calls.
Try to use new mock for each test, usually done by configuring the mock in a beforeEach hook.
Edit
You can use .toHaveBeenNthCalledWith which specifies which call is under test.

My overall problem was whitespace. Plus I had to change the way the underlining executeQuery function was being called in the actual controller. So the expect statements in the third image are actually correct. I copied the sql statement from the controller and pasted in the test. I did NOT add tabs and it worked.

Related

Convert all functions to asynchronous quickly in VS Code

I'm building a web app. For my back-end, I ended up creating many different functions. Only later— when I started setting up my API— did I realize that I wanted all my functions to be asynchronous.
I probably should've had better foresight, but now I am stuck with many different functions that I do not want to manually change. The only progress I made was to find all instances of def and replace it with async def. This worked but as I am working with Python, the problem is with the await keyword.
Is there any quick way to convert all functions like this
example_function(arguments)
to this?
await example_function(arguments)
I want to quickly add await to all functions regardless of the name. This is a problem as my functions have varying names and replacing most common instances would break my app. I am working with Python in VS Code.
Turn on regex matching for the editor find-and-replace feature, then use the following search input:
([a-zA-Z_][a-zA-Z0-9-_]*|[a-zA-Z_]+)\(.*\)
and the following replacement field:
await $0
You could maybe use the "Replace All" action (/ button or keyboard shortcut), but I'd be more confident going through it one by one. There might be false positives. I highly doubt every single function call you make in a file is to an asynchronous function (especially calls to ones that you didn't write).
The $0 in the replacement field means "the whole thing that was matched".

JMeter functions and variables

I'm new to JMeter so this question may sound absolutely dumb...
I have a loop in which a variable (let's say it is called "raw") is being changed and written to file every iteration. The variable contains HTML encoded text so it has to be converted into plain text. I found out this can be done using __unescapeHtml function. When I tried using it worked but I ended up always receiving the same text as on the first iteration. Then I learned that I have to use vars.get instead of ${} to access a variable. So I changed ${__unescapeHtml("${raw}")} to ${__unescapeHtml(vars.get("raw")} which kind of helped: vars.get is getting the new value of raw each iteration but __unescapeHtml didn't work at all now - it just returns the encoded text from raw. I didn't succeded finding anything about this exact problem so I'm kind of stuck.
Ended up using
import org.apache.commons.lang3.StringEscapeUtils
...
StringEscapeUtils.unescapeHtml4(vars.get("raw"))
Don't know if it is a good way to do this but at least it works.
I assume, that you are using the expression ${...} inside a JSR-223 sampler or similar context. The user manual for JSR-223 Sampler states, that those scripts can be cached by JMeter. That is why you only get the values from the first time the context gets created.
The same is true for simple variable evaluations as ${varname}, as for function calls like ${__unescapeHtml(...)}.
The solution here is:
don't use ${...} inside of JSR-223 contexts, that might be cached.
you can however pass those expressions (${...}) into the context by using them as parameters through the input labeled Parameters on the JSR-223 Sampler – again assuming, that you are using it.
you can use the features, that your chosen JSR-223 context gives you, as you have done, by using the StringEscapeUtils#unescapeHtml4

Mocking an Assert statement

I am teaching an introductory python course (python 3+Jupyter), and have been formulating assignments using nbgrader. For those not familiar, this basically means marking student's code via a set of assert statements. If the assert doesn't pass, they don't get the mark.
One of the tests that I want to perform is to check that students are writing their own tests. As a very simple example, let's imagine that they're supposed to be defining my_function, and all the tests that they want to run on it are supposed to be a series of assert statements inside a function do_tests() which will return True if all the tests pass.
One thing that I can obviously require of their code is that do_tests() passes simply by calling assert do_tests(). I can also check that it fails if I del my_function. However, I also want to check a bit more detail about the content of do_tests(). As a first step, I simply wanted to count the number of assert statements that they have used within the definition, and was intending to use unittest.mock.patch, trying to adapt the code from here. However, I could not figure out how to mock assert. I tried something like
from unittest.mock import patch
with patch('__main__.assert') as mock_assert:
do_tests()
but I just get an error that main does not have a method assert, and couldn't work out what module assert should be a part of.
As a crude interim, I have ended up doing
import inspect
lines = inspect.getsource(do_tests)
assert lines.count("\n assert")>=3,"You don't appear to have enough assert statements"
but obviously that doesn't give me access to any other features that mocking might offer.

Is there an equivalent OR logic based from a Variable value in Origen?

I am working on Verigy 93K test program and I have a logic that I would like to know if there's an equivalent code in Origen.
I am working on Verigy 93K test program and I have this logic (IF condition) that I need to insert in my flow.
Basically, I have a variable called 'INSERTION' and this will have different values like 'GCORR', 'VCORR' and others.
I would like to know if there's an equivalent code like this in Origen.
I attached a snapshot, hope that it can help clarify my question more.
In this logic, I would like to check the INSERTION value and if the value is not equal to GCORR or VCORR, the logic should pass, else, fail.
Here is the screenshot:
This pull-request adds an official API for this.
This example would be implemented as:
whenever_any ne(:INSERTION, 'GCORR'), ne(:INSERTION, 'VCORR') do
# Your tests in here
end
That would produce something logically equivalent and which can be re-targeted to other platforms.
If you don't care about that and want to produce exactly as you have it in the above example, then this should work too (where the OR is hard-coded for V93K syntax):
whenever ne(:INSERTION, 'GCORR|VCORR') do
# Your tests in here
end
Here is the preliminary documentation of this feature from the above PR - https://github.com/Origen-SDK/origen_testers/blob/66345c9422d9fa6b2577af20110259e45c2bdd26/templates/origen_guides/program/flowapi.md.erb#L71
I couldn't find api support on flow control or variable values beyond "if/unless_enable" support which can help check for 1 or zero. One way is to use render.
render 'if #INSERTION != "GCORR|VCORR" then'
render '{'
# your code for non-GCORR_VCORR flow
render "} \n else \n { \n } "

Added an extra variable to a VBA function - now has compile error stating Expected:=

I have a VBA function that worked fine, until I tried to pass an extra variable to it. Now the code won't run, and I get an error stating Expected:=, I've tried renaming the function, but no help.
Was - Function GetData(site_add)
Changed to Function GetData(site_add, temporary) and failed - despite changing the call to the function accordingly...!?!
Is it possible that the compiler is glitching and I should focus on that? I have other functions in the code that use 5 call 5 variables and don't even call/use them all...!? Help...
By adding the second parameter, you are effectively telling the compiler that every call to this method now requires two parameters instead of one. So you have to find everywhere you call the GetData() function and make sure it now passes two parameters instead of one, even if the second parameter is Nothing. Now, if you want it to default to nothing so you don't need to pass it you can rewrite it as
GetData(site_add, Optional temporary)
*my vb is rusty, so take my example with a grain of salt please.

Resources