How to clear PHPUnit's setExpectedException? - testing

How do you clear the excepted exception after setting the expectedException like so:
PHPUnit_Framework_TestCase::setExpectedException('Acme\Services\Forms\FormValidationException');
What I want to say to PHPUnit is: don't expect an exception anymore, please fail the test if you encounter one.

A unit test should only verify one aspect of the unit of code under test. So if you first want to expect some exception to be raised and then you don't that just means that you should implement these verifications in two separate tests.

Just call the same method again passing null as Exception argument:
\PHPUnit_Framework_TestCase::setExpectedException(null);

Related

Mockito throws error with Kotlin when verifying with argument matchers or captors

When using Mockito with Kotlin, if I try to verify Mock calls, it work fine like (this is in a Spring test):
#MockBean
lateinit var fragmentProcessor: FragmentProcessor
verify(fragmentProcessor, timeout(20000)).processFragment(expectedFragment)
that gives the expected behaviour... but just doing something like:
verify(fragmentProcessor, timeout(20000)).processFragment(Mockito.eq(expectedFragment))
will give the following error:
Missing method call for verify(mock) here:
-> at uk.co.argos.productapi.services.kafka.KafkaConsumerServiceTest.testFragmentProcessorReceivesMessages(KafkaConsumerServiceTest.kt:47)
Example of correct verification:
verify(mock).doSomething()
Also, this error might show up because you verify either of: final/private/equals()/hashCode() methods.
Those methods *cannot* be stubbed/verified.
Mocking methods declared on non-public parent classes is not supported.
the same happens with ArgumentCaptor or other matchers
Are you sure that you call fragmentProcessor.processFragment(expectedFragment) somewhere in your code before verify times out?
Error message says that you don't, so verify throws an exception (as it should do).
In this line:
verify(fragmentProcessor, timeout(20000)).processFragment(expectedFragment)
You don't use verify correctly (you have to use Mockito.eq): it doesn't verify anything so doesn't throw, but it doesn't mean that it works as you suppose.

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.

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 do I mark tests as Passed/Skipped/Ignored in serenity?

I keep getting phantomjs error - unreachablebrowserexception.
I want to mark the test as skipped or passed in the catch block of this managed exception. How do I do that?
You can use JUnit's Assume class at the beginning of your test to mark the test as ignored/skipped based on a condition.
For example:
#Test
public void myTest() {
Assume.assumeTrue(yourBooleanCondition);
// continue your test steps...
}
You can read about its different applications here.
However, being able to mark a failed test as passed is against the test mantra and defeats the purpose of developing such code entirely. I do not know any framework that would allow you to do that. If you absolutely have to, my guess is to fiddle with the results.

OCMock report error from stub

I want to test that my code properly handles a particular error returned from dataWithContentsOfFile. The problem is that the error isn't specifically returned from the method, it is an output parameter (passed in as NSError **.
It is easy enough for me to simply have the mock return nil from the stub, but I want to specifically test that the error it outputs is handled. How can I achieve that?
I think what you're looking for is described in section 2.5 (Returning values in pass-by-reference arguments) in the documentation: http://ocmock.org/reference/#stubing-methods