So i have a piece of code:
General.helpers.elementContains = function(selector, value) {
return driver.findElement(webdriver.By.css(selector)).getInnerHtml().then(function(contents) {
assert.equal(contents, value);
});
};
I would like to stub out the getInnerHtml function. I have currently stubbed out both the driver.findElement and the webdriver.By.css functions. My driver.findElement function returns a promise which i use the node module sinon-stub-promise.
sinon.stub(driver, 'findElement').returnsPromise();
sinon.stub(webdriver.By, 'css');
However when running the test as i am unsure of how to stub the .getInnerHtml function i get an error:
driver.findElement(...).getInnerHtml is not a function
I have tried changing the driver.findElement to return a getInnerHtml method which is stubbed and returns a value but i cannot seem to crack this one.
sinon.stub(driver.findElement).returns({getInnerHtml: sinon.stub().returns(value)})
Any help would be appreciated.
I have figured out a solution to this question:
Stub promise
promise = sinon.stub().returnsPromise();
sinon.stub(driver, 'findElement').returns({getInnerHtml: promise});
This works for me as getInnerHtml returns a promise and driver.findElement returns a object with getInnerHtml in which is a function which returns a promise.
Related
I'm trying to mock some functions using cmocka:
void test_led_driver_NeedToImplement(void **state)
{
state_t current = CLEAR;
will_return(led_status_toggel,SET);
TEST_ASSERT_EQUAL(SET, led_status_toggel(current));
}
But, I get an error: led_status_toggel() has remaining non-returned values.
Do I have to create a mock file for my functions or what's the source of this error?
Ps: I'm using unity.h as an assertions library.
According to your test, it seams that function you are testing is led_status_toggel. If that is the case, you should not mock it. You should just remove will_return(led_status_toggel,SET);, since your led_status_toggel is probably something like this (You dind't share it so I don't know exactly):
state_t led_status_toggel(state_t state)
{
if (state == CLEAR)
{
return SET;
}
return CLEAR;
}
If your function under test is not led_status_toggel, but some other (that you didn't mentioned) which calls this led_status_toggel, then you can mock this function like this
state_t __wrap_led_status_toggel(state_t state)
{
return (state_t)mock();
}
and use -Wl,--wrap=led_status_toggel in your build command. With --wrap linker flag, when you execute your test, the mock function __wrap_led_status_toggel will be called instead of the original led_status_toggel.
I just upgraded the phpunit 7.5.20 to phpunit 9.5.0 and I'm facing a lot of errors (good ones actually), but not 100% sure how to workaround with some of those errors.
Just looking for some ideas to fix the following error:
Method setDummyStuff may not return value of type NULL, its return declaration is "void"
It happens only when you're creating a createConfiguredMock() and passing a null method as a argument.
Here's my test:
<?php
use Lib\IDummyCode;
class DummyTest extends PHPUnit\Framework\TestCase
{
public function setUp(): void
{
parent::setUp();
}
public function testDummyThatReturnsVoid()
{
$this->createConfiguredMock(IDummyCode::class, [
'setDummyStuff' => null
]);
}
}
And here's the dummy class:
<?php
namespace Lib;
interface IDummyCode
{
public function setDummyStuff(
int $testInt,
string $testString
): void;
}
Do you guys, have some thoughts about how to improve this?
Thanks a lot!
The second parameter to createConfiguredMock takes an associative array where the key is the method to mock and the value is the value the method should return. Since the setDummyStuff method can not return anything (void return type) it does not make sense to define a return value. It isn't the null value in particular. It will fail with any value.
So you could just leave that method out:
$mock = $this->createConfiguredMock(IDummyCode::class, []);
Which can also be written in a nicer way:
$mock = $this->createStub(IDummyCode::class);
If you need to verify that setDummyStuff was called, you have to set up an expectation.
$mock = $this->createMock(IDummyCode::class);
$mock->expects(self::once())
->method('setDummyStuff')
->with(123, 'Hello');
I'm having trouble utilizing Powermock, Mockito, and Kotlin to mock up a return value when
the function signature that needs overriden contains varargs.
Function to mock/override:
public BoundStatement bind(Object... values);
Test File:
class DbSessionTest {
var preparedStatement: PreparedStatement? = null
#Mock
var boundStatement: BoundStatement? = null
#Before
fun setup() {
initMocks(this)
whenever(preparedStatement?.bind(any())).thenReturn(boundStatement)
// Also, have tried it like this
whenever(preparedStatement?.bind(any<Array<Any>>())).thenReturn(boundStatement)
}
}
The fuction in the actual code is called like so:
internal fun createBoundStatement(query: String, vararg params: Any): BoundStatement {
val preparedStatement = prepare(query)
val boundStatement = preparedStatement.bind(*params)
return boundStatement
}
When I step through and the varargs are dereferenced, it turns into an Object[].
When the object array contains all the same type, everything works fine, but when
it contains type String and type Integer, the mock fails to happen and null is
returned.
Note: I have also included com.nhaarman.mockito_kotlin.any package for their specific
any function and anyVararg, but that did not work either.
What is the proper way to mock this so that no matter the type in the Object array,
it will always return the mocked value?
Nicholas Hauschild's answer make me think about removing powermock and just going with regular mockito and junit separately and that worked. Removing powermock and Upgrading mockito to v2.18.3 fixed the issue.
I'm trying to create a (pure) constructor function and a QUnit test for it:
//app.js
function myFunc(param1, param2) {
this.param1 = param1;
this.param2 = param2;
return this;
}
//test.js
QUnit.test("should return a string and a number", function(assert) {
assert.ok(myFunc("some-string", 4545435234), "a string and a number were returned");
});
The code runs and passes my test until I add "use strict" to app.js. Then QUnit displays the following fail message:
1. Died on test #1 at http://some/url/:1:1: Cannot set property 'param1' of undefined
Source: TypeError: Cannot set property 'param1' of undefined
I can get both the code to work and the test to pass if I return the myFunc parameters as an array:
function myFunc(param1, param2)) {
return [param1, param2];
}
But that just doesn't seem right. I get that this has something to do with var hoisting but I'm not clear about it.
Thanks in advance.
...
In strict mode JavaScript functions are not given the default context (this), thus you must provide the context. Once way to do this is through the new keyword. If you change your assertion to the following I think this will work:
assert.ok(new myFunc("some-string", 4545435234), "a string and a number were returned");
so this is the class that i want to test. and specifically i just pick one of the function that i want to test. while var is a value returned from doing some function from classB bar is instance from classC and then do some function which pass some variables. for most of the hints/example, the function to be tested is return a value. so my question is, how to test that this particular function worked?
thanks.
class mA extends A {
...
function doSomething($foo) {
$var = doStuffFromClassB("hallo");
$bar = ClassC::instance();
$bar->doStuffFromClassC($var, $foo, "world");
}
}
If it's called doSomething and it doesn't indicate what it does by returning a value, then you can use mock objects to trace the interaction with the other objects.
See PhpUnit's documentation on mock objects. I guess in this case you want to verify that the doStuffFromClassC method is involved with the var from doStuffFromClassB.