0

私は現在、 Postmanと呼ばれる優れた REST クライアントと連携して動作するオープンソースの API テスト スイートに取り組んでいます。

テスト スイートを使用して、一連の API 呼び出しを実行し、予期される JSON オブジェクト/配列 (またはその他のもの) に対する応答をチェックします。応答/期待値が動的であることを可能にしながら、2つを比較するための優れた構造を持っていると思います。

(口ひげのような) 構文で応答の動的部分を表すことができるようにすることで、動的部分を把握しました。基本的に、値がフォーマットされ{{content}}ている場合、それに応じて処理されます。ここでの異なる値はNOT_NULL、 、STRINGARRAYOBJECT、およびNUMBERです。

この関数の sudo コードは次のようになります。

function (response, expected) {
    if expected has `{{}}` syntax {
        switch syntax
            check response type of against expected type of ({{type}})
                set flag if failure
                return
    } else {
        if response is the same type as expected {
            switch based on type of response
                case string
                    check if response/request are equal
                    set flag if failure
                    return
                case array
                    check if response/request are equal
                    set flag if failure
                    return
                case object
                    for key in object
                        call this function with response[key] and request[key]
        } else {
            set flag (failure)
            return
        } 
    } 
}

これで問題なく動作すると思いますが、このチェッカーを思いついたときに見落としていた明らかな点がないか、またはこのかなり複雑なチェックをより効率的に行う方法があるかどうかを確認したかったのです。

前もって感謝します!

アップデート

sudo コードを調べたところ、期待した結果が得られない厄介なチェックが行われたようです。上記の関数は更新され、正常に動作するようになりました。

4

1 に答える 1

0

少しの調査と試行錯誤の後、これが私が思いついたものです。おそらく完璧ではありませんが、他に提案がないため、私が行っているのは次のとおりです。

checkJSON: (response, expected) ->
    if /{{2}(STRING|NOT_NULL|ARRAY|OBJECT|NUMBER|IGNORE)}{2}/.test(expected)
        switch expected
            when '{{STRING}}'
                console.log 'testing string!'
                unless typeof(response) is 'string'
                    console.log 'TYPE OF WAS SUPPOSED TO BE STRING: '+ typeof(response)
                    @failure = true
                    return
            when '{{NOT_NULL}}'
                console.log 'testing not null!'
                unless response
                    console.log 'TYPE OF WAS SUPPOSED TO BE NOT_NULL: '+ typeof(response)
                    @failure = true
                    return
            when '{{ARRAY}}'
                console.log 'testing array!'
                unless Object::toString.call(response) is '[object Array]'
                    console.log 'TYPE OF WAS SUPPOSED TO BE OBJECT (ARRAY): '+ typeof(response)
                    @failure = true
                    return
            when '{{OBJECT}}'
                console.log 'testing object!'
                unless typeof(response) is 'object'
                    console.log 'TYPE OF WAS SUPPOSED TO BE OBJECT: '+ typeof(response)
                    @failure = true
                    return
            when '{{NUMBER}}'
                console.log 'testing number!'
                unless typeof(response) is 'number'
                    console.log 'TYPE OF WAS SUPPOSED TO BE NUMBER: '+ typeof(response)
                    @failure = true
                    return
            when '{{IGNORE}}'
                console.log 'IGNORING THIS OUTPUT!'
            #not really necessary because of regex, but good to check
            else
                console.log 'HOW DID YOU GET HERE?!?!?!'
                @failure = true
                return
    else
        if typeof(response) is typeof(expected)
            switch typeof(response)
                when 'string', 'number'
                    unless response is expected
                        console.log 'Response did not equal Expected!'
                        @failure = true
                        return
                when 'object'
                    if Object::toString.call(response) is '[object Array]'
                        #compare arrays
                        unless response.length is expected.length
                            console.log response.length +' was supposed to equal '+ expected.length
                            @failure = true
                            return
                        #need to do more checks, but this will work for now...
                        #LINK: http://stackoverflow.com/questions/7837456/comparing-two-arrays-in-javascript
                    else
                        for key of response
                            if typeof(expected[key]) != 'undefined'
                                @checkJSON(response[key], expected[key])
                            else
                                console.log 'KEY WAS INCORRECT!'
                                @failure = true
                                return
        else
            @failure = true
            return

上記のコードは、興味のある方は、coffeeScript に含まれています。

于 2013-08-06T17:02:02.347 に答える