Grails 2.0:如何在单元测试中正确使用模拟

时间:2012-11-15 13:42:00

标签: unit-testing grails

我是否必须在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 ?
}

2 个答案:

答案 0 :(得分: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 ?
}

答案 1 :(得分:0)

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