mocking - Mock method reported as called 0 times when it seems it MUST be being called at least once -


i have method i'm mocking reported being called 0 times, though seems should being called 1 times.

here's test class method (simplified):

function testfailedpasswordlogin() {     $badpassword = 'not'.$this->testpassword;     // setup fake user repo returns our test user (since we're passing valid username, incorrect password)     $this->users = $this->getmock('users', array('getbyusername'));     $this->users->method('getbyusername')->with($this->testuser->username)->willreturn(null);     $this->auth = new auth($this->users);     $this->assertfalse($this->auth->login($this->testuser->username,$badpassword));     $loggedinuser = $this->auth->user();     $this->assertequals(null, $loggedinuser); } 

update: decided try using mock framework (prophecy):

$this->users = $this->prophesize('users'); $this->users->getbyusername($this->testuser->username)->shouldbecalled(); $this->auth = new auth($this->users->reveal()); 

and same failure.

my class being tested (also simplified have method , constructor):

class auth {      public function __construct(users $users) {         $this->users = $users;         $this->sessionkey = 'auth.user';     }      public function login($username, $password) {         $user = $this->users->getbyusername($username);         if ($user && ($user->password == $this->users->hashpassword($password))) {             $_session[$this->sessionkey] = $user;             return true;         }         else return false;     }      /** logged-in user **/     public function user() {         if (empty($_session[$this->sessionkey])) return null;         else return $_session[$this->sessionkey];     }  } 

when running, receive error (slightly edited show correctly here):

there 1 failure:

1) authtest::testfailedpasswordlogin expectation failed method name equal string:getbyusername when invoked 1 time(s). method expected called 1 times, called 0 times.

failures! tests: 5, assertions: 16, failures: 1.

i pull out hair on one. admittedly getting started using mocks can't life of me figure out why mock method isn't being called.

i looked @ both phpunit mocked method called 0 times while should called once , phpunit - symfony2 : method expected called 1 times, called 0 times, , neither seems address specific problem.

i running php 5.3.10 , phpunit 4.7.3.

any can provide appreciated keeping sanity. additional information might helpful, let me know. i'm going keep researching myself far see no reason shouldn't work based on documentation.


Comments

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -