【问题标题】:Unit Testing show action of a controller with findwhere in grails单元测试在 grails 中使用 findwhere 显示控制器的操作
【发布时间】:2012-01-04 23:26:47
【问题描述】:

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

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]
        }
    }

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

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);
   } 

我已经尝试了大多数事情,但无法解决它。我错过了什么?谢谢。

【问题讨论】:

  • 无关,但Addresses.findWhere(id:Long.parseLong(params.id)); 太冗长了;只需使用Addresses.get(params.id),因为它是等效的,并且会将参数转换为正确的类型
  • 天哪,是的,这是正确的。但这是我只应该测试而不是更改的代码。伤心。
  • 您的测试在哪里失败?它是在您的测试中的 a1.save() 还是在控制器中的 Address.findWhere(..)?
  • 你能把你做mockDomain()的方法贴出来吗?您没有提到 Organizations 的模拟域。
  • 我正在为组织和地址设置 mockDomain()。

标签: unit-testing grails groovy


【解决方案1】:

在 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

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2013-06-15
  • 2014-09-14
  • 1970-01-01
  • 2020-09-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-28
相关资源
最近更新 更多