« Return to Thread: new returns null?

Re: new returns null?

by Ken T. :: Rate this Message:

Reply to Author | View in Thread

On Fri, 03 Jul 2009 21:50:38 -0500, Jeff Brown wrote:

> Can you post the source code for StoryControllerTests and Story?
>

Whoops you needed StoryControllerTests:


import org.jsecurity.crypto.hash.Sha1Hash
import org.jsecurity.subject.Subject
import org.jsecurity.SecurityUtils
import grails.test.*

class StoryControllerTests extends ControllerUnitTestCase {
    def category1
    def category2
    def normalUser

    protected void setUp() {
        super.setUp()

        mockDomain(User)
        mockDomain(Category)
        mockDomain(Story)
        mockDomain(Rating)

        // A normal user.
        normalUser = new User(
            username: "phil",
            passwordHash: new Sha1Hash("password").toHex(),
            email: "phil@...",
            firstName: "Phil",
            lastName: "Terry",
            birthDate: new Date(),
            memberSince: new Date()).save()

        // Mock a logged in user
        def subject = [
            isAuthenticated: true,
            getPrinciple: normalUser.username
        ] as Subject
        SecurityUtils.metaClass.static.getSubject = {-> return subject }

        // Set up some categories
        category1 = new Category(name: "Science Fiction", adult: false)
        category1.save();
        category2 = new Category(name: "Historical Fiction", adult: false)
        category2.save();
        new Category(name: "Erotica", adult: true).save()

    }

    protected void tearDown() {
        super.tearDown()
    }

    void testSaveStory() {
        mockRequest.method = 'POST'

        mockParams.title = "My Story"
        mockParams.description = "The story of my life"
        mockParams.text = "My life has been long and good."
        mockParams.adult = false
        mockParams.author = normalUser
        mockParams.categories = [category1, category2]

        def model = controller.save()

        assertEquals 'story', redirectArgs.controller
        assertEquals 'show', redirectArgs.action

        def story = Story.findByTitle("My Story")

        assertEquals "The story of my life", story.description
        assertEquals "My life has been long and good", story.text
        assertEquals false, story.adult
        assertEquals normalUser, story.author
        assertEquals([category1, category2] as Set, story.categories as
Set)

    }
}



--
Ken T. <ktectropy@...>


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


 « Return to Thread: new returns null?