1

我是否必须在 2.0 中将域类保存到这样的模拟表中:

def jdoe = new User(name:"John Doe", role:"user")
def suziq = new User(name:"Suzi Q", role:"admin")
def jsmith = new User(name:"Jane Smith", role:"user")

mockDomain(User, [jdoe, suziq, jsmith])

def test = User.get(1) //correct ?

或者仅仅使用 @Mock 和 @TestFor 就足够了吗?

@TestFor(MyController)
@Mock([User,Role])
{...

def jdoe = new User(name:"John Doe", role:"user")
def suziq = new User(name:"Suzi Q", role:"admin")
def jsmith = new User(name:"Jane Smith", role:"user")

def test = User.get(1) //will this work ?
}
4

2 回答 2

1

在单元测试中对域对象和 mixin 使用刷新!

@TestFor(MyController)
@Mock([User,Role])
@TestMixin(DomainClassUnitTestMixin)
{...

    def jdoe = new User(name:"John Doe", role:"user").save(flush:true)
    def suziq = new User(name:"Suzi Q", role:"admin").save(flush:true)
    def jsmith = new User(name:"Jane Smith", role:"user").save(flush:true)

    def test = User.get(1) //will this work ?
}
于 2012-11-15T20:54:20.620 回答
0

您的第二次尝试@Mock应该有效。

于 2012-11-15T13:53:04.593 回答