我正在学习圣杯,读“行动中的圣杯”( grails In Action )。试着做一些测试,但我有奇怪的行为。我有下一个简单的集成测试:
@Test
public void testProjections() throws Exception {
User user1 = new User(mail: 'test1@test.tld', password: 'password1').save(flush: true)
User user2 = new User(mail: 'test2@test.tld', password: 'password2').save(flush: true)
assertNotNull(user1)
assertNotNull(user2)
// Chain add Tag to Post
user1.addToPosts(new Post(content: 'First').addToTags(new Tag(name: 'tag-0')))
// Separate add tag to post
Post post = user1.posts.iterator().next()
Tag tag1 = new Tag(name: 'tag-1')
post.addToTags(tag1)
// http://stackoverflow.com/questions/6288991/do-i-ever-need-to-explicitly-flush-gorm-save-calls-in-grails
// Have tried with and without next line without success:
//sessionFactory.getCurrentSession().flush()
assertEquals(['tag-0', 'tag-1'], user1.posts.iterator().next().tags*.name.sort()) // line 154
…
}然后我运行了两次之后:
grails>
grails> test-app -rerun -integration
| Running 5 integration tests... 2 of 5
| Failure: testProjections(com.tariffus.QueryIntegrationTests)
| java.lang.AssertionError: expected:<[tag-0, tag-1]> but was:<[tag-1]>
at org.junit.Assert.fail(Assert.java:88)
at org.junit.Assert.failNotEquals(Assert.java:743)
at org.junit.Assert.assertEquals(Assert.java:118)
at org.junit.Assert.assertEquals(Assert.java:144)
at com.tariffus.QueryIntegrationTests.testProjections(QueryIntegrationTests.groovy:154)
| Completed 5 integration tests, 1 failed in 0m 0s
| Tests FAILED - view reports in /home/pasha/Projects/grails/com.tariffus/target/test-reports
grails>
grails> test-app -rerun -integration
| Running 5 integration tests... 2 of 5
| Failure: testProjections(com.tariffus.QueryIntegrationTests)
| java.lang.AssertionError: expected:<[3, 1, 2]> but was:<[[tag-1, tag-2, tag-0, tag-5, tag-3, tag-4], [tag-6]]>
at org.junit.Assert.fail(Assert.java:88)
at org.junit.Assert.failNotEquals(Assert.java:743)
at org.junit.Assert.assertEquals(Assert.java:118)
at org.junit.Assert.assertEquals(Assert.java:144)
at com.tariffus.QueryIntegrationTests.testProjections(QueryIntegrationTests.groovy:164)
| Completed 5 integration tests, 1 failed in 0m 0s
| Tests FAILED - view reports in /home/pasha/Projects/grails/com.tariffus/target/test-reports
grails> 正如您所看到的,第一次失败在第157行,第二次失败之后,在第二次没有任何修改的情况下运行更进一步。
我使用Postgres数据库和配置为dataSource的dbCreate = 'update‘模式的环境测试。
我做错了什么,为什么工作,,有时是
发布于 2014-02-10 08:53:11
我想说的是,问题的一个根源是这句话:
user1.addToPosts(new Post(content: 'First').addToTags(new Tag(name: 'tag-0')))这些动态addTo*方法在父实例上调用save()之前不会将保存传播到关联实例。因此,调用user1后的save()应该会修复它:
user1.addToPosts(new Post(content: 'First').addToTags(new Tag(name: 'tag-0')))
user1.save()这应该首先将save()传播到Post实例,然后将其传递到标记实例。
https://stackoverflow.com/questions/21662209
复制相似问题