1

I am using Laravel 5.0 to create phpunit test alongside the actual model. I get errors in phpunit tests but no errors when controller calls the model and it returned the desired data.

sample.php

<?php namespace App;
use Illuminate\Database\Eloquent\Model;


class sample extends Model  {


/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'sample';

/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = ['id','username','details','image'];

/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/

public static function test()
{
    return "Returned Text.";
}
public static function gettest()
{
    return self::test();
}

public static function getItem()
{
    return self::orderBy('username','asc')->get();
}

public static function findItem($id)
{
    return self::find($id);
}

}

SampleTest.php

<?php namespace App;

use Mockery as m;

class SampleTest extends \PHPUnit_Framework_TestCase {

protected function setUp()
{
    $this->mock = m::mock('App\sample')->makePartial();
}

protected function tearDown()
{
    m::close(); 
}

/** @test */
public function should_return_string()
{
    $response = $this->mock->test();
    var_dump("test() returns :".$response);
}

/** @test */
public function should_return_string_from_test_function()
{
    $response = $this->mock->gettest();
    var_dump("gettest() returns :".$response);
}

/** @test */
public function should_return_mocked_data()
{
    $this->mock->shouldReceive('test')->andReturn('Return Mocked Data');
    $response = $this->mock->gettest();
    var_dump("gettest() returns :".$response);
}

/** @test */
public function should_return_some_data_using_this_mock()
{
    $this->mock->shouldReceive('get')->andReturn('hello');
    $response = $this->mock->getItem();
}

}

Problem When I use controller to call the model, it returned the desired data. When I run phpunit on command prompt:-

  1. test function is not mocked properly as it still returns the original string
  2. getItem and findItem functions return an error saying

    1) App\SampleTest::should_return_some_data_using_this_mock BadMethodCallException: Static method Mockery_0_App_sample::getItem() does not exist on this mock object

Question How can I mock the function properly? Why it is saying the error code as shown above? Where was I doing it wrong?

Any help will be much appreciated.

Note: Test assertions is removed and replaced with var_dump to see the output on the command prompt.

4
  • This is not a PHPUnit question but rather a Mockery question. Jun 20, 2015 at 6:37
  • No need to apologize, just wanted to help you get an answer quicker :-) Jun 20, 2015 at 7:50
  • Why are you trying to mock a class which you're currently testing? Jun 23, 2015 at 6:56
  • This is the replicate of the codes I encountered problems with mocking. Actual code for test function always returns static data; an array. However when I tried to mock as shown above, it still returns the actual data instead of the mocked data.
    – wfaa
    Jun 23, 2015 at 7:50

0

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.