Results 1 to 3 of 3

Thread: addTo* not working with mockDomain

Threaded View

  1. #1
    Join Date
    Aug 2010
    Posts
    3

    Default addTo* not working with mockDomain

    I'm having a problem getting addTo* method to work properly with mocked domains.

    I'm using Win7 x64, java 1.6.0.21 and grails 1.3.4.

    Here are my test classes (Foo and Bar are domain, and Alpha is a service)

    Code:
    package alpha
    
    class Foo {
    
        String name
        
        static hasMany = [ bars : Bar ]
        
        static constraints = {
        }
        
        String toString() {
        	name
        }
    }
    Code:
    package alpha
    
    class Bar {
    
        String name
        
        static belongsTo = [ foo : Foo ]
        
        static constraints = {
        }
        
        String toString() {
        	name
        }
    }
    Code:
    package alpha
    
    class AlphaService {
    
        static transactional = true
    
        def load() {
        
        	def foo1 = new Foo(name:"foo1")
        	foo1.addToBars new Bar(name:"bar11")
        	foo1.addToBars new Bar(name:"bar12")
    		foo1.save()
    		
        	def foo2 = new Foo(name:"foo2")
        	foo2.addToBars new Bar(name:"bar21")
        	foo2.addToBars new Bar(name:"bar22")
    		foo2.save()
    		
        }
    }
    Code:
    package alpha
    
    import grails.test.*
    
    class AlphaServiceTests extends GrailsUnitTestCase {
        
        def service
        
        protected void setUp() {
            super.setUp()
    
    		mockDomain Foo
    		mockDomain Bar
    
            service = new AlphaService()
        }
    
        protected void tearDown() {
            super.tearDown()
        }
    
        void testLoad1() {
    		
    		service.load()
    		
    		assertEquals 2, Foo.list().size()
    		assertEquals 4, Bar.list().size()  // fails - expected:<4> but was:<0>
    		
        }
    
        void testLoad2() {
    		
        	def foo1 = new Foo(name:"foo1")
        	foo1.addToBars new Bar(name:"bar11")
        	foo1.addToBars new Bar(name:"bar12")
    		foo1.save()
    		
        	def foo2 = new Foo(name:"foo2")
        	foo2.addToBars new Bar(name:"bar21")
        	foo2.addToBars new Bar(name:"bar22")
    		foo2.save()
    		
    		assertEquals 2, Foo.list().size()
    		assertEquals 4, Bar.list().size()  // fails - expected:<4> but was:<0>
    		
        }
    
        void testLoad3() {
    		
        	def foo1 = new Foo(name:"foo1")
        	foo1.addToBars new Bar(foo:foo1, name:"bar11")
        	foo1.addToBars new Bar(foo:foo1, name:"bar12")
    		foo1.save()
    		
        	def foo2 = new Foo(name:"foo2")
        	foo2.addToBars new Bar(foo:foo2, name:"bar21")
        	foo2.addToBars new Bar(foo:foo2, name:"bar22")
    		foo2.save()
    		
    		assertEquals 2, Foo.list().size()
    		assertEquals 4, Bar.list().size()  // fails - expected:<4> but was:<0>
    		
        }
    
        void testLoad4() {
    		
        	def foo1 = new Foo(name:"foo1")
    		foo1.save()
    
        	foo1.addToBars new Bar(name:"bar11")
        	foo1.addToBars new Bar(name:"bar12")
    		foo1.save()
    		
        	def foo2 = new Foo(name:"foo2")
    		foo2.save()
    
        	foo2.addToBars new Bar(name:"bar21")
        	foo2.addToBars new Bar(name:"bar22")
    		foo2.save()
    		
    		assertEquals 2, Foo.list().size()
    		assertEquals 4, Bar.list().size()  // fails - expected:<4> but was:<0>
    		
        }
    
        void testLoad5() {
    		
        	def foo1 = new Foo(name:"foo1")
        	foo1.addToBars new Bar(name:"bar11").save()
        	foo1.addToBars new Bar(name:"bar12").save()
    		foo1.save()
    		
        	def foo2 = new Foo(name:"foo2")
        	foo2.addToBars new Bar(name:"bar21").save()
        	foo2.addToBars new Bar(name:"bar22").save()
    		foo2.save()
    		
    		assertEquals 2, Foo.list().size()
    		assertEquals 4, Bar.list().size()  // fails - expected:<4> but was:<0>
    		
        }
    
        void testLoad6() {
    		
        	def foo1 = new Foo(name:"foo1")
        	foo1.addToBars new Bar(foo:foo1, name:"bar11").save()
        	foo1.addToBars new Bar(foo:foo1, name:"bar12").save()
    		foo1.save()
    		
        	def foo2 = new Foo(name:"foo2")
        	foo2.addToBars new Bar(foo:foo2, name:"bar21").save()
        	foo2.addToBars new Bar(foo:foo2, name:"bar22").save()
    		foo2.save()
    		
    		assertEquals 2, Foo.list().size()
    		assertEquals 4, Bar.list().size()  // success!
    		
        }
    
    }
    When I run the tests, only testLoad6 passes. All of the other tests fail, indicating that no Bar objects have been saved.
    Is there something I'm doing wrong with my code?
    Can someone verify that they see the same problem?

    Thanks,
    Paul Woods
    Attached Files Attached Files

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •