作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在我的项目中,我有很多彼此非常相似的 Realm 对象。在设计测试时,我意识到很多测试用例是相同的。因此,我开始创建一个BaseSpec,使我能够重用逻辑和代码。
class Product{
String productName
static constraints = {
name nullable: false, blank: false, unique: true
}
}
class Category{
String categoryName
static constraints = {
name nullable: false, blank: false, unique: true
}
}
@Mock([Product, Category]) //? is this needed
class BaseSpec extends Specification {
@Shared classForTest
@Shared constructor
@Shared nameParam
def setup(){
//This is what I would override in my child Tests
//classForTest = new Category().getClass()
//constructor = classForTest.getConstructor()
//nameParam = "categoryName"
}
def "Testing that the name field is unique"(){
given: "A class instance"
String testValue = "ABSCSD SDS SDS"
def instance1 = constructor.newInstance() //using the constructor to get an instance
instance1."${nameParam}" = testValue
instance1.save(flush: true, failOnError: true)
and: "A duplicated Instance"
def instance2 = constructor.newInstance()
instance2."${nameParam}" = testValue
expect: "The second instance should not save"
shouldFail {instance2.save(flush: true, failOnError: true)}
}
//...Other common test cases
}
@TestFor(Product)
class ProductSpec extends BaseSpec{
@Override
def setup(){
classForTest = new Product().getClass() //Get the class that is testec
constructor = classForTest.getConstructor() //Get the constructor of the tested class
nameParam = "productName" //Establish the name of the parameter
}
//... Other Product Tests
}
@TestFor(Category)
class CategorySpec extends BaseSpec {
@Override
def setup(){
classForTest = new Category().getClass()
constructor = classForTest.getConstructor()
nameParam = "categoryName"
}
//... Other Category Tests
}
最佳答案
这是我最初发布的替代方法。 它不使用继承,但是将允许测试用例非常相似的测试用例。
Consider the domain classes
class Product{
String productName
static constraints = {
name nullable: false, blank: false, unique: true
}
}
class Category{
String categoryName
static constraints = {
name nullable: false, blank: false, unique: true
}
}
@Mock([Product, Category])
class CatalogSimpleGenericSpec extends Specification {
def "Validation of test field in a given domain"(){
given: "A new instance of the domain"
def testInstance = constructorOfInstance.newInstance() //Creating the instance using the constructor
testInstance."${fieldName}" = fieldValue
testInstance.validate()
expect: "Testing if the instance should save depending on the value of shouldHaveErrors"
assert testInstance.hasErrors() == shouldHaveErrors //Verifying if it should have errors
//If there are errors the instance should fail to save, otherwise it will save perfectly
shouldHaveErrors ?
shouldFail {testInstance.save(flush: true, failOnError: true)}
: testInstance.save(flush: true, failOnError: true)
where:
constructorOfInstance | shouldHaveErrors | fieldName | fieldValue | reasoning
//----------------- PRODUCT ----------------------
new Product().getClass().getConstructor() | false | "productName" | "Folders" | "Should pass since a product name is supplied"
new Product().getClass().getConstructor() | true | "productName" | "" | "Should fail empty strings should not be accepted"
new Product().getClass().getConstructor() | true | "productName" | null | "Should fail null values shoult not be accepted"
//----------------- CATEGORY --------------------
new Category().getClass().getConstructor() | false | "categoryName" | "Office Supplies" | "Should pass since a category name is supplied"
new Category().getClass().getConstructor() | true | "categoryName" | "" | "Should fail empty strings should not be accepted"
new Category().getClass().getConstructor() | true | "categoryName" | null | "Should fail null values shoult not be accepted"
}
}
testInstance."${fieldName}" = fieldValue
分配的。然后,我们使用
shouldHaveErrors 的值来测试保存是否失败。
关于grails - 在Spock Framework中继承。我想在多个规范中重用测试用例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35782540/
我是一名优秀的程序员,十分优秀!