测试返回的断言比预期的多

时间:2018-10-16 11:05:49

标签: php laravel phpunit

我已经看过有关TDD的视频,并想尝试一下。我已经设置了2个非常基本的测试,它们都工作正常,并返回了我期望的结果。问题是,为什么第二个测试返回2个断言?第一个返回了一个我所期望的断言。

public class Test {

    public static void main(String... args) {
        CompletableFuture<ComplexObject> completableFuture = CompletableFuture
                                                    .supplyAsync(()-> ComplexObject.builder())
                                                    .thenApply(cob -> cob.withComplexProp1(restService1.getDetails()))
                                                    .thenApply(cob -> cob.withComplexProp2(restService2.getDetails()))
                                                    .thenApply(cob -> cob.withComplexPropN(restServiceN.getDetails()))
                                                    .thenApply(cob -> cob.build());
        try {
            ComplexObject co = completableFuture.get();
        } catch (Exception e) {
            System.err.println("could not build the complex object");
        }
    }


}


class ComplexObject {
    private Object complexProp1;
    private Object complexProp2;
    private Object complexPropN;

    private ComplexObject() {}

    public static ComplexObjectBuilder builder() {
        return new ComplexObjectBuilder();
    }

    public static class ComplexObjectBuilder {

        private Object complexProp1;
        private Object complexProp2;
        private Object complexPropN;

        private ComplexObjectBuilder() {

        }

        public ComplexObjectBuilder withComplexProp1(Object complexProp1) {
            // process the received complexProp1 before setting it into the builder
            this.complexProp1 = complexProp1;
            return this;
        }
        public ComplexObjectBuilder withComplexProp2(Object complexProp1) {
            // process the received complexProp2 before setting it into the builder
            this.complexProp2 = complexProp2;
            return this;
        }
        public ComplexObjectBuilder withComplexPropN(Object complexProp1) {
            // process the received complexPropN before setting it into the builder
            this.complexPropN = complexPropN;
            return this;
        }

        public ComplexObject build() {
            ComplexObject co = new ComplexObject();
            co.complexProp1 = this.complexProp1;
            co.complexProp2 = this.complexProp2;
            co.complexPropN = this.complexPropN;
            return co;
        }
    }
}

运行此测试文件会得到:

  

Sebastian Bergmann和贡献者的PHPUnit 7.4.0。
     .. 2/2(100%)
     时间:187毫秒,内存:14.00MB
     OK(2个测试,3个断言)

1 个答案:

答案 0 :(得分:3)

似乎assertRedirect实际上在内部执行了两个断言。一项检查if the status code is a redirect code,另一项检查if the redirected url is correct