测试状态变更规则

发布于 2024-10-15 13:52:44 字数 284 浏览 0 评论 0原文

我在域模型中有一个实体。该实体具有 ChangeStatus(StatusesEnum newStatus) 方法。 StatusesEnum 有超过 10 个成员。它们只能按正确的顺序更改,例如 StatusesEnum.First 可以更改为 Second 和 Third,Third 只能更改为 Fourth,Fourth 可以更改为 Fifth 和 Seventh 等。

ChangeStatus 方法会因错误而引发异常过渡。 我不认为测试所有情况都是正确的(也许我错了:)),但是我如何编写测试来检查正确的转换顺序。

I have an entity in Domain Model. This entity has ChangeStatus(StatusesEnum newStatus) method.
StatusesEnum has more then 10 members. They can be changed only in correct order, for example StatusesEnum.First can be changed to Second and Third, Third can be changed to Fourth only, Fourth can be changed to Fifth and Seventh etc.

ChangeStatus method is throwing exception for wrong transitions.
I don't think that testing all cases is right (maybe I'm wrong :)), however how can I write test for checking right transitions order.

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

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

发布评论

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

评论(2

单调的奢华 2024-10-22 13:52:44
> how can I write test for checking right transitions order.

如果您使用的是 NUnit 2.5 或更高版本,您可以使用
参数化测试

    [Test, Sequential]
    public void MyTest(
        [Values(State1,State2,State99)] StatusesEnum initialState,
        [Values(State2,State3,State1)] StatusesEnum nextState,
        [Values(true, true, false)] bool expectedOutcome
        )
    {
        Entity entity = new Entity(initialState);
        if (expectedOutcome)
            entity.ChangeStatus(nextState); // this should be ok
        else {
            try {
                entity.ChangeStatus(nextState); 
                                              // this should throw
                Assert.Fail("Missing expected Exception");
            } catch (IllegaleStateTransitionException ex) {
                // every thing is ok
            }
        }

        ...
    }

但是,如果我是你,我会创建一个非-抛出将在状态逻辑中使用的决策函数

    IsValidTransition(StatusesEnum oldState, StatusesEnum newState)

,并在单元测试中进行测试,因此不再需要
测试异常逻辑

> how can I write test for checking right transitions order.

If you are using NUnit 2.5 or later you can use
Parameterized Tests

    [Test, Sequential]
    public void MyTest(
        [Values(State1,State2,State99)] StatusesEnum initialState,
        [Values(State2,State3,State1)] StatusesEnum nextState,
        [Values(true, true, false)] bool expectedOutcome
        )
    {
        Entity entity = new Entity(initialState);
        if (expectedOutcome)
            entity.ChangeStatus(nextState); // this should be ok
        else {
            try {
                entity.ChangeStatus(nextState); 
                                              // this should throw
                Assert.Fail("Missing expected Exception");
            } catch (IllegaleStateTransitionException ex) {
                // every thing is ok
            }
        }

        ...
    }

However if i were you i would create a non-throwing decision-function

    IsValidTransition(StatusesEnum oldState, StatusesEnum newState)

that is to be used in you statelogic and is tested in unittest so there is no more need to
test the exception logic

风流物 2024-10-22 13:52:44

你想有多彻底取决于你自己。我会对每个正确的转换进行测试,例如

[Test]
public void ChangeStatus_FirstToSecond_Succeeds()
{
     Entity entity = new Entity(StatusEnum.First);
     entity.ChangeStatus(StatusEnum.Second);
     Assert.That(entity.Status, Is.EqualTo(StatusEnum.Second);
}

对每个输入状态的一个无效转换进行测试:

[Test]
[ExpectedException(typeof(StatusChangeException))]
public void ChangeStatus_FirstToTenth_ThrowsException()
{
     Entity entity = new Entity(StatusEnum.First);
     entity.ChangeStatus(StatusEnum.Tenth);
}

How thorough you want to be is up to you. I'd have tests for each of the correct transitions, e.g.

[Test]
public void ChangeStatus_FirstToSecond_Succeeds()
{
     Entity entity = new Entity(StatusEnum.First);
     entity.ChangeStatus(StatusEnum.Second);
     Assert.That(entity.Status, Is.EqualTo(StatusEnum.Second);
}

And a test for one invalid transition per input state:

[Test]
[ExpectedException(typeof(StatusChangeException))]
public void ChangeStatus_FirstToTenth_ThrowsException()
{
     Entity entity = new Entity(StatusEnum.First);
     entity.ChangeStatus(StatusEnum.Tenth);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文