简体   繁体   English

Grails 域单元测试 class (gorm)

[英]Grails unit test of domain class (gorm)

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. 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?结果是[]和null? What did I do wrong?我做错了什么?

Also, I would like also to see the SQL generated by GORM (Hibernate) behind the scenes.另外,我还想看看 GORM (Hibernate) 在幕后生成的 SQL。 Any idea how I might do that in Grails?知道如何在 Grails 中做到这一点吗?

don't do this: client.id =1;不要这样做:client.id =1;

save() will supply an id. save() 将提供一个 id。

you may need to save(flush:true).您可能需要保存(刷新:真)。

just do the save and use then use the id to do a get.只需保存并使用,然后使用 id 进行获取。

then do your testing.然后做你的测试。

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

http://www.ibm.com/developerworks/java/library/j-grails10148/index.htmlhttp://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." “正如我之前提到的,Grails 支持两种基本类型的测试:单元测试和集成测试。两者之间在语法上没有区别——两者都使用相同的断言编写为 GroovyTestCase。区别在于语义。单元测试旨在测试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 -* 命令生成相应的集成测试,所以大多数人只是使用已经存在的东西。正如您稍后将看到的,您想要测试的大多数东西都需要完整的环境才能启动并运行,所以集成测试是一个很好的默认值。”

First, you shouldn't be evaluating GORM itself.首先,您不应该评估 GORM 本身。 Those who provide Grails take on the responsibility for testing GORM.提供 Grails 的人承担测试 GORM 的责任。 Granted, you probably didn't mean that anyway.诚然,你可能不是那个意思。

Second, testing findBy*() is not usually the concern for unit tests.其次,测试 findBy*() 通常不是单元测试的关注点。 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().如果确实需要测试 findBy*(),则需要收集所有 findBy*() 响应实例并将该列表作为第二个参数传递给 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.您在示例中错误地使用了 mockDomain() - 您必须告诉 mockDomain() 要模拟哪些实例才能在 findBy*() 调用中接收它们。

Saving the client can fail without an exception being thrown, which would explain why res is null.保存客户端可能会失败而不会引发异常,这可以解释为什么 res 是 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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM