Returning a mock object from a mock object

I’m trying to return an object when mocking a parser class. This is the test code using PHPUnit 3.7

    //set up the result object that I want to be returned from the call to parse method
    $parserResult= new ParserResult();
    $parserResult->setSegment('some string');        

    //set up the stub Parser object
    $stubParser=$this->getMock('Parser');
    $stubParser->expects($this->any())
               ->method('parse')
               ->will($this->returnValue($parserResult));

    //injecting the stub to my client class
    $fileWriter= new FileWriter($stubParser);
    $output=$fileWriter->writeStringToFile();

Inside my writeStringToFile() method I’m using $parserResult like this:

writeStringToFile(){
   //Some code...
    $parserResult=$parser->parse();
    $segment=$parserResult->getSegment();//that's why I set the segment in the test.
}

Should I mock ParserResult in the first place, so that the mock returns a mock?
Is it good design for mocks to return mocks?

Is there a better approach to do this all?!

In general having a chain of mocks returning other mocks is considered a code smell in tests.

That being said, the answer of whether this is good idea is of course “it depends”.

About your ParserResult object – if it is a small value object then constructing it and passing it in is probably a good idea.

If it is a large object with dependancies of its own, then mocking it would be a better option. Not mocking it would increase the coupling in your tests.

Another question to ask about the design is what is the result of ParserResult.getSegment used for? Perhaps a Tell Don’t Ask style might be better, can you tell ParserResult or Parser to do something with the segment information instead of extracting it and then operating on it?

2

I think it’s a good idea to mock the smallest and closest possible set of dependencies to the class you are testing.
The idea is to reduce the test into the simplest possible scenario so it’s easy to ensure the test is correct and that only tests one thing.

If you return a mock you are testing two separate things, the function that consumes the mock and the class that now creates and returns the mock.

So test the consumer and the producer of the ParserResult separately.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật