knocker_d
knocker_d

Reputation: 586

Grails 2.0: how to use mocking correctly in unit test

Do I have to save a domain class to a mock table like this in 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 ?

Or is it enough just to use @Mock and @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 ?
}

Upvotes: 1

Views: 328

Answers (2)

jenk
jenk

Reputation: 1043

use flushing in unit tests for domain objects and 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 ?
}

Upvotes: 1

Tomasz Kalkosiński
Tomasz Kalkosiński

Reputation: 3723

Your second attempt with @Mock should work.

Upvotes: 0

Related Questions