单元测试原则 ORM 模型

发布于 2024-10-08 09:13:50 字数 751 浏览 0 评论 0原文

我认为这是一个非常愚蠢的问题,但是如果您已经使用 ORM 框架,您会测试您的模型吗?当我意识到没有太多可测试的东西时,我就这么做了?我主要只是使用模型来获取值?例如。添加用户

$user = new User();
$user->username = "user1";
$user->password = "password";
$em->persist($user);
$em->flush();

然后编辑将是类似的

$user = /* get user */
$user->email = "[email protected]";
$em->flush();

事情。我可能会添加的唯一功能是注册用户和注册用户。更改密码以处理密码加盐和验证用户名可用。

也许另一件事是吸气剂和吸气剂。设置器?是一件微不足道的事情吗?你会怎么做?

public function testCanSetUsername() {
    $user->username = "Hello";
    $this->assertEquals("Hello", $this->username);
}

就这样吗?

I think this is quite a stupid question, but do you test your Models if you already use a ORM framework. I was at it when I realized there isn't much to test? I mainly will just use the models for values? eg. for adding a user

$user = new User();
$user->username = "user1";
$user->password = "password";
$em->persist($user);
$em->flush();

then edit will be similar

$user = /* get user */
$user->email = "[email protected]";
$em->flush();

something like that. The only functionality that I will probably add is register user & change password to handle password salting & verification that username is available.

Maybe another thing is getters & setters? Quite a trivial thing? How will you do that?

public function testCanSetUsername() {
    $user->username = "Hello";
    $this->assertEquals("Hello", $this->username);
}

Just like that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

晒暮凉 2024-10-15 09:13:50

您应该编写故事测试来确保代码能够满足用户的需求。听起来像是一个简单的说法,但这些是最重要的测试。这些真正测试应用程序做什么而不是如何做。您会看到这些测试最终覆盖了您问题中的代码。

例如,您可能有这样的测试:

  • 用户注册到网站
  • 用户更改密码
  • 等。

事实上,如果您编写了所有这些测试并且有些代码没有被它们覆盖,您可能不需要那段代码,并且如果您使用 TDD,那么一开始就不会编写它。

You should write story tests that ensure that the code does what the user wants. It sounds like a simple statement, but these are the most important tests. These really tests what the app does rather than how. You'll see that these tests end up covering the code you have in your question.

For example, you may have tests like:

  • user registers to website
  • user changes password
  • etc.

In fact, if you write all these tests and there is code that is not covered by them, you probably don't need that piece of code, and would not have written it in the first place if you had used TDD.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文