5

findOrCreateByを使用してオブジェクトを検索したり、一致するオブジェクトが見つからない場合はインスタンス化したりしようとしていますが、期待どおりに機能しません。

これは私が持っているものです:

String myBaz = "some unique string"
FooType myFooType = FooType.findByName("Large")

// The Foo table is empty, so this should give me a new Foo
Foo myFoo = Foo.findOrCreateByBazAndFooType(myBaz, myFooType)

assert myFoo.baz == myBaz 
assert myFoo.fooType == myFooType // Fails because myFoo.fooType is null, 
// but should be set to myFooType

私は何を間違っていますか?fooTypeが正しく設定されていないのはなぜですか? これは予期された動作ですか、それとも Grails のバグですか?

4

1 に答える 1

1

よくわかりませんが、これをテストとして実行しようとしているようです。(あなたの主張に基づいて)

Grails フレームワークによって追加された動的メソッドは、ドメイン クラスをモックしない限り、単体テストでは使用できません。これは別の質問サイトから取得した古い grails コードですが、役立つ場合があります

import grails.test.GrailsUnitTestCase   

class MessageControllerTests extends GrailsUnitTestCase {   

    def savedMessages   

    void setUp() {   
        super.setUp()   
        savedMessages = []   
        mockDomain(Message, savedMessages) //mocking the domain class   
        mockController(MessageController) //mocking the controller   
    }   

    void testMessageCanBeCreated() {   
        def messageController = new MessageController()   
        messageController.params.title = 'detail'  
        messageController.params.detail = 'some detail'  

        messageController.save() // executing the save action on the MessageController   

        assertEquals('list', messageController.redirectArgs.action)   
        assertEquals(1, savedMessages.size()) //assert the message has been saved   
    }   
}  
于 2012-07-17T18:31:32.217 に答える