I've been trying to figure out mocking of class and having it's method return what I want.
So in every XUnit lib you have something like this:
class Car
{
private service;
public function create()
{
if(!$this->service->isValid()) {
return false;
}
/** more code **/
return true;
}
public function setService($service)
{
$this->service = $service;
}
}
class CarTest extends TestCase
{
public function testCreate()
{
$serviceMock = $this->getMock(Service::class);
$serviceMock->expects($this->once())->method('isValid')->willReturn(true);
$car = new Car();
$car->setService($serviceMock);
$this->assertTrue($car->create());
}
}
So let's get back to JS and JEST.
I have same situation in JS
class Car {
set_service(service) {
this.service = service;
}
create() {
if (!this.service.validate()) {
return false;
}
/** more code **/
return true;
}
}
How would I mock service class because that is also a class in another file and how would I set validate method to return true.
There is no clear example in documentation that I get, maybe I am just too used to XUnit libraries to understand this new concept.
Aucun commentaire:
Enregistrer un commentaire