У меня есть тест по каратэ, который проходит:
Scenario Outline: Find all the the prime factors in the range from <start> to <end> are <result>
Given path '/primefactors'
And param start = <start>
And param end = <end>
When method get
Then status 200
And match header content-type contains 'application/json'
And match header content-type contains 'charset=utf-8'
And match response == {numbers:<result>, start:<start>, end:<end>, count:<count>, type:PrimeFactors}
Examples:
| start | end | result | count
| 8 | 10 | {8: [2,2,2], 9:[3,3], 10:[2,5]} | 3
| 13 | 16 | {13: [13], 14:[2,7], 15:[3,5], 16:[2,2,2,2]} | 4
Однако я хотел бы, чтобы у меня не было переменной count из раздела «Примеры:», и я просто вычисляю количество по длине числа.ключей в переменной объекта результата следующим образом:
Scenario Outline: Find all the the prime factors in the range from <start> to <end> are <result>
Given path '/primefactors'
And param start = <start>
And param end = <end>
When method get
Then status 200
And match header content-type contains 'application/json'
And match header content-type contains 'charset=utf-8'
And def result = <result>
And match response == {numbers:<result>, start:<start>, end:<end>, count:'#(Object.keys(result).length)', type:PrimeFactors}
Examples:
| start | end | result
| 8 | 10 | {8: [2,2,2], 9:[3,3], 10:[2,5]}
| 13 | 16 | {13: [13], 14:[2,7], 15:[3,5], 16:[2,2,2,2]}
Когда я пытаюсь это сделать, я получаю сообщение об ошибке:
primefactors.feature:33 - javascript evaluation failed: Object.keys(result).length, TypeError: {8=[2,2,2], 9=[3,3], 10=[2,5]} is not an Object in <eval> at line number 1
и тест не пройден.
Учитывая, чтоObject.keys(result).length
является действительным JS (с использованием консоли разработчика Chrome):
result = {8: [2,2,2], 9:[3,3], 10:[2,5]}
{8: Array(3), 9: Array(2), 10: Array(2)}
Object.keys(result).length
3
Что я делаю не так?Как правильно это сделать?
ОБНОВЛЕНИЕ (9 апреля 2019 г.) Успешно работает следующее:
Background:
* url baseUrl
* configure lowerCaseResponseHeaders = true
* def keys = function(o){ return o.keySet() }
* def values = function(o){ return o.values() }
* def size = function(o){ return o.size() }
Scenario Outline: Find all the the prime factors in the range from <start> to <end> are <result>
Given path '/primefactors'
And param start = <start>
And param end = <end>
When method get
Then status 200
And match header content-type contains 'application/json'
And match header content-type contains 'charset=utf-8'
And def result = <result>
And match response == {numbers:<result>, start:<start>, end:<end>, count: '#(size(result))', type:PrimeFactors}
Examples:
| start | end | result
| 8 | 10 | {8: [2,2,2], 9:[3,3], 10:[2,5]}
| 13 | 16 | {13: [13], 14:[2,7], 15:[3,5], 16:[2,2,2,2]}