繁体   English   中英

单元测试显示了控制器的动作,其中findwhere位于grails

[英]Unit Testing show action of a controller with findwhere in grails

我正在尝试对控制器的显示动作进行单元测试。 它的定义是:

def show = {
        //scs 11/22/2011 limit access to users in their Organization
        def currentOrgViewCheck = session.currentUserOrganizationId.viewAllPost;
        def currentOrg = session.currentUserOrganizationId;

        def addressesInstance ;
        if(currentOrgViewCheck){
            addressesInstance = Addresses.findWhere(id:Long.parseLong(params.id));
        }else{
            addressesInstance = Addresses.findWhere(id:Long.parseLong(params.id), organization:currentOrg);
        }


        if (!addressesInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'addresses.label', default: 'Addresses'), params.id])}"
            redirect(action: "list")
        }
        else {
            [addressesInstance: addressesInstance]
        }
    }

我试图通过模拟Addresses域来测试它,然后创建并保存一个Addresses实例,最后将其ID传递给控制器​​的参数。 但是测试没有通过。
我也尝试在单元测试方法上测试findWhere方法,无论我给它提供什么参数,它都一直说失踪方法异常,对于给定的数据类型不存在这样的签名。 我完全被困住了。 这是我的测试代码:

void testShowFound()
   {
       // New Controller to test
       ac = new AddressesController();

       // this instance is required both to test as well as a session variable
       Organizations org = new Organizations(name:'test', phone: '352-999-8888', createdBy: creator, modifiedBy: modifier, viewAllPost: true);
       org.save();

       //Create an address to pass its id to the action as parameter.
       Addresses a1 = new thlc.Addresses(firstLine:'A1', secondLine:'B', thirdLine:'C', luCountry:UnitedStates, zipCode:'12345', luState:Florida, city:'jag');
       a1.save();   

       ac.metaClass.getParams = {->[id:a1.id] }
       //ac.params.id = a1.id; // I tried both ways.



        // This findWhere is giving error, can any one explain why ?
       //def a = Addresses.findWhere(id:a1.id);
       //assertEquals(a, a1);

       //session variables mocked.
       mockSession['currentUserOrganizationId'] = org;

       // This is to bypass the flash message problem, cause it is not possible to
       // mock the message method on flash messages.
       //ac.metaClass.message = { Map map -> return "ByPass Message" }

       //Call the action and test
       def model = ac.save();
       assert(model);

       //Testing redirect, This is the test that is failing
       assertEquals("list", redirectParams.action);
   } 

我已经尝试了大多数事情,但是无法解决。 我想念什么? 谢谢。

似乎在Grails 1.3.7(及更低版本)中, mockDomain不会创建findWhere()动态查找器。 从它的声音来看,如果要使用它,您必须自己模拟该方法。 我还没有自己做。

参考文献:

http://www.slideshare.net/tlberglund/test-first-refresh-second-testdriven-development-in-grails (幻灯片38)

http://grails.1312388.n4.nabble.com/New-approach-to-mocking-domain-classes-in-Grails-unit-tests-td2529895.html

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM