CPPUTEST: How to ignore only one mocked call placed between other ones - testing

I would like to ignore one call placed between other ones that I want to call under the same case test. If I use ignoreothercalls I have not clear if the rest of the calls, following this, will be called. I need the rest, after ignored call, will be called. Or at least, to find the way of stopping ignoreothercalls effect before the end of the test case.
TEST(group, test1){
...
mock().expectOneCall("HAL_AS393x_CommandStrobes").withParameter("cCommandCode",AS393X_CMD_CALIB_RCO_LC);
/*-------------------------------------------------------*/
mock().expectOneCall("HAL_AS393x_ReadRegisters");//I want ignore only this mocked function call
/*-------------------------------------------------*/
mock().expectOneCall("HAL_AS393x_Deinit");
...
}
I would like to leave this call without being tested, and remove it from de test case without gettig expecting calls errors for it:
mock().xxxCall("HAL_AS393x_ReadRegisters"); //-where xxxCall = unkown keyword used for this-

You have to check expectation in between:
mock().expect...
do_something_that_call_mocks();
mock().checkExpectations();
/* Start all over again */
Complete example using checkExpectations() to reset mock() here

Related

Calling OCMStub and OCMReject on the same method

I've been attempting to write some fail fast tests using OCMReject. However I've found that if OCMStub is used in conjunction with OCMReject, this test will pass
id _mockModel = OCMProtocolMock( #protocol( CTPrefModelProtocol));
//It doesn't seem to matter what order these two are in, the test behaves the same
OCMStub([_mockModel getPreferences]);
OCMReject([_mockModel getPreferences]);
[_mockModel getPreferences];
Even though it should clearly fail because I'm calling the function that I've set in the OCMReject method.
I realise I can just stub getPreferences whenever I'm expecting a result from it and remove it from this test, but largely that means if I've set a stub on getPreferences in my setUp method, any test that calls OCMReject([_mockModel getPreferences]) will just be ignored.
Why am I not able to use OCMStub and OCMReject together? Is it because OCMStub alters getPreferences somehow and as a result whenever I call this method, it actually calls some other method instead?
So apparently I can't read. Reading through the OCMock 3 Documentation, under the limitations heading 10.2
Setting up expect after stub on the same method does not work
id mock = OCMStrictClassMock([SomeClass class]);
OCMStub([mock someMethod]).andReturn(#"a string");
OCMExpect([mock someMethod]);
/* run code under test */
OCMVerifyAll(mock); // will complain that someMethod has not been called
The code above first sets up a stub for someMethod and afterwards an
expectation for the same method. Due to the way mock objects are
currently implemented any calls to someMethod are handled by the stub.
This means that even if the method is called the verify fails. It is
possible to avoid this problem by adding andReturn to the expect
statement. You can also set up a stub after the expect.
I suspect this same limitation exists for OCMReject as well. Hopefully this helps equally blind people like myself. A link to the documentation for the lazy.

How to use OCMock to verify that an asynchronous method does not get called in Objective C?

I want to verify that a function is not called. The function is executed in an asynchronous block call inside the tested function and therefore OCMReject() does not work.
The way I have tested if async functions are indeed called would be as follows:
id mock = OCMClassMock([SomeClass class]);
OCMExpect([mock methodThatShouoldExecute]);
OCMVerifyAllWithDelay(mock, 1);
How would a test be done to test if a forbidden function is not called?
Something like:
VerifyNotCalled([mock methodThatShouoldExecute]);
OCMVerifyAllWithDelay(mock, 1);
I would recommend using an OCMStrictClassMock instead of the OCMClassMock (which gives you a nice mock). A strict mock will instantly fail your test if any method is called on it that you did not stub or expect, which makes your tests a lot more rigorous.
If that's not an option for you, you can do what you described with:
OCMReject([mock methodThatShouoldExecute]);
See the "Failing fast for regular (nice) mocks" section in the OCMock docs.
Now as for waiting for your code which may call the forbidden method, that's another matter. You can't use OCMVerifyAllWithDelay since that returns immediately as soon as all expectations are met, it doesn't wait around a full second to see if illegal calls will be made to it. One option is to put a 1 second wait before verifying the mock each time. Ideally, you could also wait explicitly on your asynchronous task with an XCTestExpectation. Something like:
XCTestExpectation *asyncTaskCompleted = [self expectationWithDescription:#"asyncTask"];
// Enqueued, in an onCompletion block, or whatever call
// ... [asyncTaskCompleted fulfill]
[self waitForExpectationsWithTimeout:1 handler:nil]

Call functions from smart contract

could I interact with functions in my deployed contract without using truffle-contract?
I just want to run and play with my functions to check how they work.
I used MyContract.at("0x...").MyFunctionName(parameters,{from:"x0..."});
but it doesn't work.
Any idea or suggestions
Thanks
It's hard to know what you mean by "it's doesn't work", but I guess is that you are not seeing any output when running MyContract.at("0x...").MyFunctionName(parameters,{from:"x0..."}); in the truffle console?
If yes, the reason is because invoking a method to an instance of a contract will give you a Future, and you must handle the result coming back from the call in an asynchronous way. For example, if the function return a value indicating that some computation has happened, you can print the returned value in the console by:
MyContract.at("0x...").MyFunctionName(parameters,{from:"x0..."}).then(console.log)
If you're writing unit tests (to be executed via truffle test), then you can handle the return value by doing this:
MyContract.at("0x...").MyFunctionName(parameters,{from:"x0..."}).then(function(returnedValue) {
// do something with the returnedValue, e.g.
// assert.equal(returnedValue, 3, "The returned value must be 3");
});

How to suppress to perform a webdynpro-method from the pre-exit

I want to check condition in pre exit method, if that condition is false then I have to come out of the method, without calling original method.
As far as I know you cannot. However, you have some options:
Check the original method and see if there is a variable you can set in the pre-exit so the logic in the method is not processed;
Use an implicit enhancement at the top of the method you want to skip, you put the code here that you were planning to put in the pre-exit;
Use an overwrite-exit where you call the original method using me->method( ) whenever you do want to call it. If I remember correctly you have to create the overwrite with the option to have access to the global vars.

UFT- Can I get the result of an action during run?

I have created a test with 1 action which contains n actions. Is there any way to check after the execution of each action of the n actions the result if it is fail or pass and proceed accordingly?
Not directly.
A similar idea would be to ask for the current run result status, see How can I get the run result status according to the current report node?, but that is currently unsolved.
You can, however, call your actions, and consume there return value, like this:
ActionSucceeded=RunAction ("myTest [Action2]", oneIteration)
If not ActionSucceeded then
' The Action2 call signalled "failure" (false)
End If
This implies that Action2 must return such a result, like in here:
ExitActionIteration (false)
Beware, however, of the fact that RunAction statements need to be inserted using QTP´s IDE (Insert/Call To Action...), resulting in a RunAction call without brackets. When editing this to the assignment form above (with brackets), make sure you get it right the first time before you save the test -- because if you save a test containing a broken RunAction call, QTP disassociates the called test, and the test will fail at runtime even if you edit the script back to correct syntax. This is due to metainfo that QTP saves invisibly in the script, and if you save an invalid action call, this metainfo is being discarded. (You can see when this happens because the action call will disappear from the test flow view.)
And: If you don´t store the RunAction result in a variable, but use it directly, like in
If not RunAction ("myTest [Action2]", oneIteration) then
' The Action2 call signalled "failure" (false)
End If
the same mess arises: QTP does not understand that this is a valid action call, and it won´t work even if you edit it into back into the original form.
Except for the "beware" hint, the same holds true for LoadAndRunAction, which calls an action at runtime. LoadAndRunAction can be called as a function, and if the called actions returns a value via ExitActionIteration, it returns that value.
Yet another "beware" hint: ExitActionIteration really requires its arguments to be enclosed in brackets, even though it is a Sub (or at least called as a Sub). I suspect this is because it is not a real Sub or Function, but a special statement "patched into" the MS VBScript engine in some exotic way.