如何使用Fancordion验证返回HTTP 404的RESTful服务?

时间:2015-02-12 12:17:47

标签: rest fantom afbedsheet

我用Fantom编程语言编写RESTful API。我正在使用Fancordion编写验收测试并拥有此方案:

Country.fandoc

Country by Code
###############
Passing a country code as a parameter to the Country RESTful service must return the corresponding country
Example
-------
- Given the URL to get the details of Andorra [/country/AD]`set:countryUrl`
- When I send a HTTP GET request to it
- Then I expect the name to be [Andorra]`verifyEq:countryByCode.name`.

CountryFixture.fan

using afBedSheet
using afBounce
using afButter
using afFancordion
using util

@Fixture { specification=`specs/Country.fandoc` }
class CountryFixture : BaseFixture {

  Str? countryUrl

  Country countryByCode() {
    server := BedServer(AppModule#).startup
    client := server.makeClient
    response := client.sendRequest(ButterRequest(`$countryUrl`)).asStr
    Str:Obj? json := JsonInStream(response.in).readJson
    country := Country.fromJson(json)
    return country
  }
}

这很有效。现在,我想验证当无效的国家/地区代码传递给请求时,RESTful服务会返回HTTP 404错误,因为我已经使用浏览器实现并验证了错误。但是,我在Fancordion文档中找不到如何验证是否返回了HTTP 404错误。我得到的是一个夹具失败。

***
*** 1 Fixtures FAILED [6 Fixtures]
***

我对此场景的验收测试是(附加到Country.fandoc):

Wrong Country Code
##################
Passing a wrong country code as a parameter to the Country RESTful service must cause it to return a HTTP 404 error

Example
-------
- Given the URL to get the details of country that does not exist [/country/XX]`set:countryUrl`
- When I send a HTTP GET request to it
- Then I expect a HTTP 404 error when I try `verifyEq:countryByCode`

我怎么能抓住404呢?

1 个答案:

答案 0 :(得分:1)

如果您正在寻找类似于verifyErr(Type errType)方法的内容,Fancordion没有。那是因为这些方法与实现密切相关,而实际上并不适合Fancordion测试的风格。

相反,请使用以下内容禁用Butter引发的404 Err:

client.errOn4xx.enabled = false

让Fancordion夹具设置响应。

Uri? url
ButterResponse? response

Void httpGet() {
  client := BedServer(AppModule#).startup.makeClient
  client.errOn4xx.enabled = false
  this.response = client.get(url)
}

然后让Fancordion规范验证状态代码:

Example
-------
- Given the URL to Unknown is [/country/XX]`set:url`
- When I send a [HTTP GET request]`exe:httpGet`
- Then I expect the returned status code to be [404]`eq:response.statusCode`.