unit testing - Mocking domain classes in Grails -
i've got set of domain , controller classes called: organization , organizationcontroller respectively.
the organizationcontroller has 1 method:
def index() { def organizations = organization.list() [orgs: organizations] }
i've tried mock out domain class 2 ways.
the first way using @mock annotation, , creating objects , saving:
void "test index"() { given: new organization(name: 'jimjim').save() new organization(name: 'abc').save() def expected = [org: [new organization(name: 'jimjim'), new organization(name: 'abc')]] when: def actual = controller.index() then: actual == expected }
that caused oraganization.list return empty list. actual returns [org: []]
i tried using mockdomain:
void "test index"() { given: mockdomain(organization, [new organization(name: 'jimjim'), new organization(name: 'abc') ]) def expected = [org: [new organization(name: 'jimjim'), new organization(name: 'abc')]] when: def actual = controller.index() then: actual == expected }
however still got same result. why domain classes not getting mocked?
my test decoration (organizationcontrollerspec) following:
@testfor(organizationcontroller) @mock(organization) @testmixin(domainclassunittestmixin) class organizationcontrollerspec extends specification {
i'm using grails 2.3.8.
the first snippet seems ok, but...
first of all, organization
objects created? required fields provided? please, try using save(failonerror: true)
make sure.
moreover, in controller have orgs
, while use org
in test. misspell?
also, unless have equals
method overwritten in organization
class, objects database not equal ones create new
operator.
Comments
Post a Comment