Grails 集成测试不会回滚

2024-02-09

我正在从这本书中学习grails”Grails 的实际应用 http://my.safaribooksonline.com/book/web-development/ruby/9781933988931“并且我正在尝试从示例中运行集成测试。在书中,它说每个集成测试函数应该在每个测试完成时回滚其操作。它不是回滚每个事务(因为当我完成时数据库是脏的) )。我试图找出原因,发现找到了一个名为“transactional”的属性。据称,您将此属性设置为 true,它将使测试用例具有事务性,但它似乎不会改变行为。我已经包含了代码对于下面的单元测试。

我正在使用 grails 1.3.7 并连接到 MySql 数据库。测试运行成功,只是不回滚。我在这个集成测试中做错了什么,它跳过了回滚吗?

UserIntegrationTests.groovy:

package com.grailsinaction

import framework.TestTools

class UserIntegrationTests extends GroovyTestCase {
    static transactional = true

    protected void setUp() {
        super.setUp()
    }

    protected void tearDown() {
        super.tearDown()
    }

    void testCreateUser() {
        TestTools.banner(log, "testCreateUser()")

        def user = new User(userId:"joe", password:"secret")
        assertNotNull user.save()
        assertNotNull user.id

        def foundUser = User.get(user.id)
        assertEquals 'joe', foundUser.userId
    }

    void testSaveAndUpdate() {
        TestTools.banner(log, "testSaveAndUpdate()")

        def user = new User(userId:"joe2", password:"secret")
        assertNotNull user.save()

        def foundUser = User.get(user.id)
        foundUser.password = 'sesame'
        foundUser.save()

        def editedUser = User.get(user.id)
        assertEquals 'sesame', editedUser.password
    }

    void testSaveThenDelete() {
        TestTools.banner(log, "testSaveThenDelete()")

        def user = new User(userId: 'joe3', password: 'secret')
        assertNotNull user.save()

        def foundUser = User.get(user.id)
        foundUser.delete()
        assertFalse User.exists(foundUser.id)
    }

    void testValidation() {
        TestTools.banner(log, "testValidation()")

        def user = new User(userId: 'chuck-norris', password: 'tiny')
        assertFalse user.validate()
        assertTrue user.hasErrors()

        def errors = user.errors
        assertNotNull errors

        errors.allErrors.each {
            log.info("field: ${it.field}, code=${it.code}, rejected=${it.rejectedValue}")
        }
    }
}

用户.groovy

package com.grailsinaction

class User {
    String userId
    String password
    Date dateCreated
    Profile profile

    static constraints = {
        userId(size: 3..20, unique: true)
        password(size: 6..8, validator: {password, user ->
            return (password != user.userId)
        })
        dateCreated()
        profile(nullable: true)
    }

    static mapping = {
        profile lazy: false
    }

    static hasMany = [posts : Post]
}

测试执行日志

Testing started at 8:28 PM ...
Welcome to Grails 1.3.7 - http://grails.org/
Licensed under Apache Standard License 2.0
Grails home is set to: C:\Users\jmquigley\workspace\apps\Grails\grails-1.3.7
Base Directory: C:\Users\jmquigley\workspace\samples\lang-grails\hubbub
Resolving dependencies...
Dependencies resolved in 963ms.
Running script C:\Users\jmquigley\workspace\apps\Grails\grails-1.3.7\scripts\TestApp.groovy
Environment set to test
  [groovyc] Compiling 1 source file to C:\Users\jmquigley\workspace\samples\lang-grails\hubbub\target\classes
    [mkdir] Created dir: C:\Users\jmquigley\workspace\samples\lang-grails\hubbub\target\test-reports\html
    [mkdir] Created dir: C:\Users\jmquigley\workspace\samples\lang-grails\hubbub\target\test-reports\plain
Starting integration test phase ...
  [groovyc] Compiling 1 source file to C:\Users\jmquigley\workspace\samples\lang-grails\hubbub\target\classes
  [groovyc] Compiling 1 source file to C:\Users\jmquigley\workspace\samples\lang-grails\hubbub\target\classes
[INFO ]20110417@20:28:42,959:grails.spring.BeanBuilder: [RuntimeConfiguration] Configuring data source for environment: TEST
  [groovyc] Compiling 1 source file to C:\Users\jmquigley\workspace\samples\lang-grails\hubbub\target\test-classes\integration
-------------------------------------------------------
Running 4 integration tests...
Running test com.grailsinaction.UserIntegrationTests...
--Output from testCreateUser--
[INFO ]20110417@20:28:46,897:groovy.util.GroovyTestCase: Test Case: testCreateUser()
--Output from testSaveAndUpdate--
[INFO ]20110417@20:28:47,534:groovy.util.GroovyTestCase: Test Case: testSaveAndUpdate()
--Output from testSaveThenDelete--
[INFO ]20110417@20:28:47,568:groovy.util.GroovyTestCase: Test Case: testSaveThenDelete()
--Output from testValidation--
[INFO ]20110417@20:28:47,642:groovy.util.GroovyTestCase: Test Case: testValidation()
[INFO ]20110417@20:28:47,668:groovy.util.GroovyTestCase: field: password, code=size.toosmall, rejected=tiny
null
PASSED
Tests Completed in 1173ms ...
-------------------------------------------------------
Tests passed: 4
Tests failed: 0
-------------------------------------------------------
[junitreport] Processing C:\Users\jmquigley\workspace\samples\lang-grails\hubbub\target\test-reports\TESTS-TestSuites.xml to C:\Users\JMQUIG~1\AppData\Local\Temp\null90011239
[junitreport] Loading stylesheet C:\Users\jmquigley\workspace\apps\Grails\grails-1.3.7\lib\junit-frames.xsl
[junitreport] Transform time: 415ms
[junitreport] Deleting: C:\Users\JMQUIG~1\AppData\Local\Temp\null90011239
Tests PASSED - view reports in target\test-reports
Application context shutting down...
Application context shutdown.

Process finished with exit code 0

默认情况下,测试(和服务)是事务性的,因此您通常只需指定静态transactional财产,当它是false。如果您没有指定方言,它可能会自动检测 MySQL,但随后会使用默认引擎(可能是 MyISAM)创建表。非事务性的 MyISAM 表。每当您使用 MySQL 时,请务必指定 InnoDB 方言,例如

test {
   dataSource {
      dialect= org.hibernate.dialect.MySQLInnoDBDialect
      driverClassName = 'com.mysql.jdbc.Driver'
      username = '...'
      password = '...'
      url = '...'
      dbCreate = 'update'
   }
}

或者,如果您在所有环境中使用 MySQL,则可以将其移至顶层,例如

dataSource {
   pooled = true
   dialect = org.hibernate.dialect.MySQLInnoDBDialect
   driverClassName = 'com.mysql.jdbc.Driver'
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Grails 集成测试不会回滚 的相关文章

随机推荐

  • 使用 Xamarin.Forms(xaml 和 c#)创建超链接

    我基本上想使用标签类在 Xamarin Forms 中创建超链接 基本上 我想通过以下链接将用户带到网络浏览器中的 google com
  • 在调试模式下调用函数时,GDB 崩溃

    我正在尝试使用 MinGW 在 Windows 上制作一个 C 程序 构建的程序运行正常 但是在调试时出现问题 调试时 如果我尝试检查函数或方法的执行结果 如下面的屏幕截图 GDB 会被强制终止 我已经将 GDB 作为命令行运行 但结果是相
  • 如何调用 sqlite3_errmsg 了解 sqlite3_prepare_v2 失败的原因

    基于C的函数sqlite3 prepare v2返回 1 我只是想以可读的形式了解错误消息并更正我的代码 我是从 raywinderlich 博客中将其作为教程学习的 我遇到过sqlite3 errmsg我不知道如何使用sqlite3 er
  • SensorManager:一个 SensorEventListener VS 多个侦听器

    我正在尝试将传感器测量结果记录到设备内存中 为此 我为许多传感器注册相同的 SensorEventListener 然后根据类型使用开关将它们分开 E g int type sensor getType switch type case S
  • 在 npm 构建期间找不到模块 @restart/context/forwardRef

    我最近开始遇到问题npm build升级到较新版本后react bootstrap 1 0 0 beta 6 Creating an optimized production build Failed to compile Cannot f
  • 从 Cordova 2.5 升级到 Cordova 3.0,在使用 CordovaInterface 时遇到问题

    我正在将我的项目从 Cordova 2 5 迁移到 Cordova 3 遵循中提到的迁移过程 http cordova apache org docs en 3 0 0 guide cli index md html http cordov
  • 从电子邮件中删除无效字符

    我想帮助用户在电子邮件输入中错误地输入无效字符 服务器端验证 清理前 注意 我不在前端验证电子邮件 只是清理 Coffeescript Element find input type email on change keyup event
  • Frederickson堆选择算法简单解释

    Frederickson 的堆选择算法是否有任何简单的解释 可以在 O k 时间内找到在线任何地方可用的最小堆中的第 k 个排序元素 如果没有 任何人都可以解释该算法的内部原理吗 尝试谷歌搜索 frederickson heap selec
  • 如何将自定义默认生成操作关联到 Visual Studio 中的自定义文件类型?

    我有一个为自定义文件类型构建的语言服务 此外 我还在 MSBuild 项目文件中创建了一个自定义目标 构建操作 但是 我无法找到任何方法将该构建操作默认关联到我的自定义文件扩展名 例如 如果添加 cs 文件 则构建操作默认为 编译 我想为我
  • php strip_tags 删除所有内容

    我在用户输入上使用 strip 标签来删除所有可能的标签 但 strip tags php 函数也会删除 例如 某些用户可能会使用表情符号 gt 或者这甚至可以在算法等时使用 是否有任何解决方案允许带状标签上的 问题是在这种情况下 foo
  • MySQL 工作台插入

    我正在使用 MySQL Workbench 5 2 28 来设计我的数据库架构 我需要将默认数据插入到一些表中 这可以使用 插入 选项卡来完成 然而 它似乎只允许手动输入数据 一次一行 我有一个包含数百行的 OUTFILE 我想插入这些行
  • React SetState 不调用 render

    我将我的函数发送到子组件callBack 在父级中 我有一个函数setState method onInputUpdated id var array let char id slice 1 console log this state s
  • ASP.NET MVC;一次只能为一名用户编辑选项

    我有一个表 其中包含三个字段和一些记录 如果用户要编辑表中的记录 则不允许其他用户同时编辑该记录 我可以采取哪些步骤来实现这一目标 许多具有桌面应用程序背景的人会想知道这是如何在 Web 应用程序中完成的 锁定记录标志 桌面世界中的一种方法
  • 如果与 ClientHttpRequestInterceptor 一起使用,Spring Resttemplate postforobject 将返回 null 作为对象响应

    我正在尝试使用休息服务 并且正在使用 Spring 发布一些数据RestTemplate postForObjectMethod但我收到空响应 即使我可以在有效负载中看到请求和响应 更新 我正在使用拦截器实现ClientHttpReques
  • CI::报告没有为 Ruby Test::Units 生成 xml?

    我正在尝试使用 CI reporter 生成 ruby 单元测试报告 我的耙文件 require rake require rake testtask require rake packagetask require rake requir
  • 两列并排可滚动

    我的页面看起来像这样 我有两个单独的 div 一个是产品过滤器 另一个是产品 div 产品内容可以显示 40 个产品或 100 个产品或无 即内容可以稍后更改 同样 我的过滤器的长度也可以变化 我希望以某种方式使过滤器 div 可滚动 并使
  • 如何将 AWS S3 url 转换为 boto 的存储桶名称?

    我正在尝试访问http s3 amazonaws com commoncrawl parse output segment http s3 amazonaws com commoncrawl parse output segment 桶与
  • OpenCL 动态并行/GPU 生成的线程?

    CUDA 5 刚刚被释放 http nvidianews nvidia com Releases NVIDIA Releases CUDA 5 Making Programming With World s Most Pervasive P
  • Stream 和 Spring Data 的优点

    有些人重写 CrudRepository 的方法 findAll 以返回 Stream java 8 但我看到他们最终将 Stream 转换为 List 以便通过其余控制器发送它 他们为什么使用 Stream 在这里使用 Stream 有什
  • Grails 集成测试不会回滚

    我正在从这本书中学习grails Grails 的实际应用 http my safaribooksonline com book web development ruby 9781933988931 并且我正在尝试从示例中运行集成测试 在书