域类 (gorm) 的 Grails 单元测试

发布于 2024-11-15 14:58:48 字数 922 浏览 3 评论 0原文

开始使用 grails,我想评估 GORM,因此我使用 Spring Tool Suite 创建了一个域类:Client 以及 namevatNumberregNumber 并自动创建测试类。

我添加的单元测试代码是:

package pilot1

import grails.test.*

class ClientTests extends GrailsUnitTestCase {
    protected void setUp() {
        super.setUp()
    }

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

    void testSomething() {
        def instances = []
        def myTestDomain = mockDomain(Client, instances)
        def client = new Client(name:"Test",vatNumber:"323",regNumber:"343")
        client.id =1;
        assertEquals client.name, "Test"
        client.save();
        def res = Client.findByName("Test")
        println instances
        println res
        //assertEquals 1, instances.size()
    }
}

结果是[]和null!我做错了什么?

另外,我还想看看 GORM (Hibernate) 在幕后生成的 SQL。知道我如何在 Grails 中做到这一点吗?

Started playing with grails and I want to evaluate GORM, so I created a domain class using Spring Tool Suite: Client with name, vatNumber, and regNumber and the test class was created automatically.

The code for unit test I added is :

package pilot1

import grails.test.*

class ClientTests extends GrailsUnitTestCase {
    protected void setUp() {
        super.setUp()
    }

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

    void testSomething() {
        def instances = []
        def myTestDomain = mockDomain(Client, instances)
        def client = new Client(name:"Test",vatNumber:"323",regNumber:"343")
        client.id =1;
        assertEquals client.name, "Test"
        client.save();
        def res = Client.findByName("Test")
        println instances
        println res
        //assertEquals 1, instances.size()
    }
}

The results are [] and null! What did I do wrong?

Also, I would like also to see the SQL generated by GORM (Hibernate) behind the scenes. Any idea how I might do that in Grails ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

冰魂雪魄 2024-11-22 14:58:49

不要这样做:client.id =1;

save() 将提供一个 id。

您可能需要保存(flush:true)。

只需保存并使用,然后使用 id 进行获取即可。

然后进行测试。

此链接可能有用: http: //blog.springsource.com/2011/06/07/countdown-to-grails-1-4-unit-testing/

don't do this: client.id =1;

save() will supply an id.

you may need to save(flush:true).

just do the save and use then use the id to do a get.

then do your testing.

this link may be useful: http://blog.springsource.com/2011/06/07/countdown-to-grails-1-4-unit-testing/

晨与橙与城 2024-11-22 14:58:49

http://www.ibm.com/developerworks/java/library /j-grails10148/index.html

“正如我之前提到的,Grails 支持两种基本类型的测试:单元测试和集成测试。两者之间没有语法差异 — 都写为使用相同断言的 GroovyTestCase 的区别在于语义。单元测试旨在单独测试类,而集成测试允许您在完整的运行环境中测试类。
坦率地说,如果您想将所有 Grails 测试编写为集成测试,那对我来说没问题。所有 Grails create-* 命令都会生成相应的集成测试,因此大多数人只是使用已有的测试。正如您稍后将看到的,您想要测试的大多数内容都需要完整的环境启动并运行,因此集成测试是一个非常好的默认设置。”

http://www.ibm.com/developerworks/java/library/j-grails10148/index.html

"As I mentioned earlier, Grails supports two basic types of tests: unit and integration. There's no syntactical difference between the two — both are written as a GroovyTestCase using the same assertions. The difference is the semantics. A unit test is meant to test the class in isolation, whereas the integration test allows you to test the class in a full, running environment.
Quite frankly, if you want to write all of your Grails tests as integration tests, that's just fine with me. All of the Grails create-* commands generate corresponding integration tests, so most folks simply use what is already there. As you'll see in just a moment, most of the things you want to test require the full environment to be up and running anyway, so integration tests are a pretty good default."

2024-11-22 14:58:49

首先,您不应该评估 GORM 本身。提供 Grails 的人承担测试 GORM 的责任。当然,你可能并不是这个意思。

其次,测试 findBy*() 通常不是单元测试的关注点。如果您确实需要测试 findBy*(),则需要收集所有 findBy*() 响应实例并将该列表作为第二个参数传递给mockDomain()。您在示例中错误地使用了mockDomain()——您必须告诉mockDomain()要模拟哪些实例,以便在findBy*()调用中接收它们。

First, you shouldn't be evaluating GORM itself. Those who provide Grails take on the responsibility for testing GORM. Granted, you probably didn't mean that anyway.

Second, testing findBy*() is not usually the concern for unit tests. If you do need to test findBy*(), you'll need to collect all of the findBy*() response instances and pass that list as the second argument to mockDomain(). You are using mockDomain() incorrectly in your example -- you must tell mockDomain() which instances to mock in order to receive them back in a findBy*() call.

转身以后 2024-11-22 14:58:49

保存客户端可能会失败而不抛出异常,这可以解释为什么 res 为空。尝试下面的代码,以便您可以查看保存客户端是否失败以及为何失败。

client.save()
if(client.hasErrors()){
// Saving failed, look in client.errors to see the specific reason
}

Saving the client can fail without an exception being thrown, which would explain why res is null. Try the following code below, so you can see if and why saving the client failed.

client.save()
if(client.hasErrors()){
// Saving failed, look in client.errors to see the specific reason
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文