Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
987 views
in Technique[技术] by (71.8m points)

php - Is it possible to create a mock outside a test case in PhpUnit?

It may seem silly, hope not, but I want to create a service that will return mock objects for people that uses my project so they can mock all the classes from my project and test their code.

My idea was to offer this kind of service so it can be called inside other project's test cases and obtain the appropriate mock for each test.

Is that possible? Or there are other ways to do that. Btw, I can't use any mocking library because of project's limitations.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Yes, it is possible. Under the hood the getMock method uses the PHPUnit_Framework_MockObject_Generator class. So you can use it directly:

PHPUnit_Framework_MockObject_Generator::getMock($originalClassName, $methods)

But you will lose all the expectation shortcuts like $this->once(). You will have to instantiate the expectations on your own:

$mock->expects(PHPUnit_Framework_TestCase::once())

Look at the PHPUnit source code to see how the mocks are build


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...