Grails/GORM:继承域类的行为不一致(测试 VS Bootstrap)

发布于 2024-12-08 17:46:44 字数 1444 浏览 0 评论 0原文

我发现我的应用程序的行为与继承域类的测试用例的结果不一致。以下是该问题涉及的类(仅提供部分代码)。

class Ticket {
    String title
    String description
    Date dateCreated
    Date lastUpdated
    User postedBy
}

class FacebookTicket extends Ticket {
    FacebookPost facebookPost
}

class FacebookPost {
    String postId
    String message
    String postedById
    String postedByName
    Date createdOnFacebook
    Date lastUpdatedOnFacebook
}

相关测试代码:

def facebookPost1 = new FacebookPost(postId:"12345", message:"This post should become a ticket", postedById:"09876",postedByName:"Rowz Roller", createdOnFacebook: new Date(), lastUpdatedOnFacebook: new Date())

facebookPost1.save(flush:true)

def facebookTicket1 = new FacebookTicket (title:"Facebook Ticket1",description:"First FACEBOOK ticket here",postedBy:user1,facebookPost: facebookPost1)

mockForConstraintsTests(FacebookTicket, [facebookTicket1])

assertTrue facebookTicket1.validate() //Succeeds

assertNotNull(facebookTicket1.save()) //Succeeds

def instances = FacebookTicket.list()
assertEquals 1,instances.size() //Fails

assertEquals 失败,并显示 junit.framework.AssertionFailedError: Expected:<1>;但是是:<0>

然而,同一段代码在 Bootstrap 中运行良好,相关实体甚至显示在 FacebookTicket 的列表视图中。

这是由于 GORM 测试继承域类的一些限制还是我在这里做错了什么? (Ticket 和 FacebookPost 测试工作正常。)

谢谢。

PS:请告诉我是否应该提供更多代码。

I am seeing an inconsistency in the behavior of my application VS the outcome of a test-case that I have for an inherited domain class. Here are the classes involved in the issue (only part of code provided).

class Ticket {
    String title
    String description
    Date dateCreated
    Date lastUpdated
    User postedBy
}

class FacebookTicket extends Ticket {
    FacebookPost facebookPost
}

class FacebookPost {
    String postId
    String message
    String postedById
    String postedByName
    Date createdOnFacebook
    Date lastUpdatedOnFacebook
}

Relevant test code:

def facebookPost1 = new FacebookPost(postId:"12345", message:"This post should become a ticket", postedById:"09876",postedByName:"Rowz Roller", createdOnFacebook: new Date(), lastUpdatedOnFacebook: new Date())

facebookPost1.save(flush:true)

def facebookTicket1 = new FacebookTicket (title:"Facebook Ticket1",description:"First FACEBOOK ticket here",postedBy:user1,facebookPost: facebookPost1)

mockForConstraintsTests(FacebookTicket, [facebookTicket1])

assertTrue facebookTicket1.validate() //Succeeds

assertNotNull(facebookTicket1.save()) //Succeeds

def instances = FacebookTicket.list()
assertEquals 1,instances.size() //Fails

The assertEquals fails with junit.framework.AssertionFailedError: expected:<1> but was:<0>

However, the same piece of code works fine in Bootstrap and the relevant entity even shows up in the list view for FacebookTicket.

Is this due to some limitation for GORM testing w.r.t. inherited domain classes or am I doing something wrong here?
(The Ticket and FacebookPost tests work fine.)

Thank you.

PS: Please let me know if I should provide more code.

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

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

发布评论

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

评论(1

旧话新听 2024-12-15 17:46:44

...有类似的问题,建议将其作为集成测试而不是单元测试来处理。问题就消失了。

... had a similar problem and was advised to to handle it as an integration test as opposed to unit test. Problems went away.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文