dpfps86064 2016-11-24 10:38
浏览 22
已采纳

测试不存在的代码[关闭]

TDD claims that I should write tests first. Say I want to write a service, how do I start writing tests for the service if I don't have anything yet at all? What would be the first test? An attempt to instantiate the service and get an exception?

PHP Laravel-based example:

class ServiceTest extends TestCase
{
    public function testServiceExists()
    {
        $service = App::make('grid');
    }
}

Result:

PHPUnit 5.5.4 by Sebastian Bergmann and contributors.

E 1 / 1 (100%)

Time: 123 ms, Memory: 14.00MB

There was 1 error:

1) ServiceTest::testServiceExists ReflectionException: Class grid does not exist

/home/supertrall/domains/md.local/laravel/vendor/laravel/framework/src/Illuminate/Container/Container.php:749 /home/supertrall/domains/md.local/laravel/vendor/laravel/framework/src/Illuminate/Container/Container.php:644 /home/supertrall/domains/md.local/laravel/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:709 /home/supertrall/domains/md.local/laravel/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php:237 /home/supertrall/domains/md.local/laravel/tests/Grid/ServiceTest.php:20

ERRORS! Tests: 1, Assertions: 0, Errors: 1.

  • 写回答

2条回答 默认 最新

  • dongping8572 2016-11-24 12:53
    关注

    Encountering such errors is a good thing in TDD. It's implying that you need to resolve the error just in order to encounter another one.

    When coding in TDD, you program by wishful thinking. In other words, you write some code that uses a component, before you implement the component itself. This helps to discover what functions and data you need, and following this discovery will lead you to more simple and useful APIs.

    Even though the TDD concepts are not very hard to learn, getting used to TDD's test-first development approach is hard and time-consuming. You're on the right track, just read more and test more to become comfortable with the methodology.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了