Domanda

Questo è quello che ho:

$observer = $this->getMock('SomeObserverClass', array('method'));
$observer->expects($this->once())
         ->method('method')
         ->with($this->equalTo($arg1));

Ma il metodo dovrebbe prendere due parametri. Sto solo testando che il primo parametro viene passato correttamente (come $ arg1).

Come testare il secondo parametro?

È stato utile?

Soluzione

Credo che il modo per farlo sia:

$observer->expects($this->once())
     ->method('method')
     ->with($this->equalTo($arg1),$this->equalTo($arg2));

o

$observer->expects($this->once())
     ->method('method')
     ->with($arg1, $arg2);

Se è necessario eseguire un diverso tipo di asserzione sul secondo argomento, è possibile farlo anche:

$observer->expects($this->once())
     ->method('method')
     ->with($this->equalTo($arg1),$this->stringContains('some_string'));

Se devi assicurarti che qualche argomento passi più asserzioni, usa logicalAnd ()

$observer->expects($this->once())
     ->method('method')
     ->with($this->logicalAnd($this->stringContains('a'), $this->stringContains('b')));
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top