SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Aug 2012
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    phpUnit and getMock

    I placed this post in another part of the forum and realized it might be the wrong place, so forgive me if you are seeing this for the second time.

    I am using phpunit. I'm a little new and have run into a tough problem.

    I am testing a function in a class whose constructor I absolutely have to disable because it news up an object that I can't access from the test. So, I use the code:

    PHP Code:
    public function testConnect(){
        
    $mock $this->getMockBuilder('FSCall')
            ->
    setMethods(array('setOpened'))
            ->
    disableAutoload()
            ->
    disableOriginalConstructor()
            ->
    getMock();
        
    $call = new FSCall;
        
    $call->connect();

    ...where 'setOpened' is a local function that function connect calls, but I don't need it to actually call the function for the unit test.

    The problem is that phpUnit returns the error "Call to undefined method FSCall::connect()". The function is in the FSCall class. Now I can "fix" the error by doing a...

    PHP Code:
    $mock->expects($this->any())->method('connects'
    ...but then the connects function doesn't actually get called.

    What am I misunderstanding here?

    Any help will be greatly appreciated.

    G

  2. #2
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by gavicus View Post
    I am testing a function in a class whose constructor I absolutely have to disable because it news up an object that I can't access from the test. So, I use the code:
    Instead of trying to fix the test case, fix the class in question to be more flexible. Plus Mock is not for use on the object that is to be tested but to feed to the object that is being tested. Meaning, you do not mock the object you are going to test.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  3. #3
    SitePoint Member
    Join Date
    Aug 2012
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks logic. That clears things up a little... and complicates matters too. I'll see what I can do about making the thing more testable. I appreciate your help.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •