将数据表与交互相结合
Combining data tables with interactions
有什么方法可以使用 Spock 的数据表来验证交互吗?
def test(int a, String b) {
expect:
service.save(a)
1 * repository.save({
def test ->
test.value == b
})
where:
a | b
1 | "one"
2 | "two"
}
是的,下面的例子工作得很好:
import spock.lang.Specification
class LolSpec extends Specification {
def 'lol'() {
given:
def repository = Mock(Repository)
def service = new Service(repository: repository)
when:
service.save(a)
then:
1 * repository.save({ it ->
it.value == b
})
where:
a | b
1 | "one"
2 | "two"
}
}
class Repository {
def save(Entity e) {
}
}
class Service {
Repository repository
def save(Integer value) {
Entity e
if (value == 1) {
e = new Entity(value: "one")
} else {
e = new Entity(value: "two")
}
repository.save(e)
}
}
class Entity {
String value
}
有什么方法可以使用 Spock 的数据表来验证交互吗?
def test(int a, String b) {
expect:
service.save(a)
1 * repository.save({
def test ->
test.value == b
})
where:
a | b
1 | "one"
2 | "two"
}
是的,下面的例子工作得很好:
import spock.lang.Specification
class LolSpec extends Specification {
def 'lol'() {
given:
def repository = Mock(Repository)
def service = new Service(repository: repository)
when:
service.save(a)
then:
1 * repository.save({ it ->
it.value == b
})
where:
a | b
1 | "one"
2 | "two"
}
}
class Repository {
def save(Entity e) {
}
}
class Service {
Repository repository
def save(Integer value) {
Entity e
if (value == 1) {
e = new Entity(value: "one")
} else {
e = new Entity(value: "two")
}
repository.save(e)
}
}
class Entity {
String value
}