出于测试目的,我需要覆盖'equals'方法:
def any = [equals: { true }] as String
any == 'should be true'
// false
更详细的问题:
class EmployeeEndpointSpec extends RestSpecification {
void "test employee" () {
when:
get "/v1/employee", parameters
then:
expectedStatus.equals(response.statusCode)
expectedJson.equals(response.json)
where:
parameters << [
[:],
[id: 824633720833, style: "small"]
]
expectedStatus << [
HttpStatus.BAD_REQUEST,
HttpStatus.OK
]
expectedJson << [
[errorCode: "badRequest"],
[
id: 824633720833,
name: "Jimmy",
email: "jimmy@fakemail.com",
dateCreated:"2015-01-01T01:01:00.000", // this value should be ignored
lastUpdated: "2015-01-01T01:01:00.000" // and this
]
]
}
}
lastUpdated
和dateCreated
可能会及时更改,我需要
不知何故忽略了它们。
答案 0 :(得分:1)
如果不需要比较上述字段 - 请删除它们:
class EmployeeEndpointSpec extends RestSpecification {
void "test employee" () {
when:
get "/v1/employee", parameters
then:
expectedStatus.equals(response.statusCode)
def json = response.json
json.remove('dateCreated')
json.remove('lastUpdated')
expectedJson.equals(response.json)
where:
parameters << [
[:],
[id: 824633720833, style: "small"]
]
expectedStatus << [
HttpStatus.BAD_REQUEST,
HttpStatus.OK
]
expectedJson << [
[errorCode: "badRequest"],
[
id: 824633720833,
name: "Jimmy",
email: "jimmy@fakemail.com",
dateCreated:"2015-01-01T01:01:00.000",
lastUpdated: "2015-01-01T01:01:00.000"
]
]
}
}
我还将测试消极和积极情景分开。
您还可以单独测试keySet()
测试键值,而不是比较整个地图。这就是我这样做的方式:
then:
def json = response.json
json.id == 824633720833
json.name == "Jimmy"
json.email == "jimmy@fakemail.com"
json.dateCreated.matches('<PATTERN>')
json.lastUpdated.matches('<PATTERN>')
如果您不喜欢最后两行,可以将其替换为:
json.keySet().contains('lastUpdated', 'dateCreated')
答案 1 :(得分:1)
答案是:
String.metaClass.equals = { Object _ -> true }