【问题标题】:Grails unit testing working in different wayGrails 单元测试以不同的方式工作
【发布时间】:2014-05-21 10:37:25
【问题描述】:

我正在使用 grails 2.3.6 版本。我有两个域类如下

class Profile {

  String name

  static belongsTo = Student
}


class Student {
Profile profile
Integer enrolmentNumber
}

我正在尝试创建一对一的单向关系。问题是我写了一个单元测试用例

@TestFor(Student)
@Mock([Profile,Student])
class StudentSpec extends Specification {

void "test save Student"() {
    when :"Student details"
    Profile profile = new Profile(name:"Test")
    Student student = new Student(enrolmentNumber: 10,profile:profile)

    then : " Student should be saved"
    assertNotNull(student.save(flush: true))
    assertNotNull(profile.id)

    when:"When I delete the student"
    student.delete()

    then:"Profile should also deleted"
    assertNull Profile.findById(profile.id)
}
}

测试在最后一行“Profile.findById(profile.id)”失败,说明找到了配置文件。根据我的理解,当我有“belongsTo”时,应该发生级联,我的意思是当我删除学生时,个人资料也应该被删除。但它没有发生。

我没有创建单元测试,而是在集成测试中使用了相同的代码,它可以正常工作。

不确定我做错了什么。或者我的理解是错误的。

【问题讨论】:

    标签: unit-testing grails groovy grails-2.0


    【解决方案1】:

    试试

       student.delete(flush:true)
    

    【讨论】:

    • 不,它不工作。我收到同样的错误提示“预期: 但原为:个人资料:1”
    猜你喜欢
    • 1970-01-01
    • 2017-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多