【问题标题】:Disable Grails plugin禁用 Grails 插件
【发布时间】:2010-11-13 20:57:52
【问题描述】:

在我的 Grails 应用程序中,我想防止在运行单元测试时加载 Searchable 插件。我尝试在 Bootstrap 类中使用以下代码来执行此操作

def grailsApplication

def init = {servletContext ->
    def currentEnv = grails.util.Environment.current.name

    if (currentEnv == 'test') {

        def doNothing = {println "Searchable is disabled"}

        // This returns null!
        def searchablePluginClass = grailsApplication.getClassForName("SearchableGrailsPlugin")

        searchablePluginClass.metaClass.doWithDynamicMethods = doNothing 
        searchablePluginClass.metaClass.doWithSpring = doNothing 
        searchablePluginClass.metaClass.doWithApplicationContext = doNothing 
    }
}

但是这不起作用,因为grailsApplication.getClassForName("SearchableGrailsPlugin") 返回 null,大概是因为当这段代码运行时,这个类不在类路径上。有没有其他方法可以禁用这个插件?

【问题讨论】:

    标签: grails grails-plugin


    【解决方案1】:

    我找到了解决方案。将以下内容添加到 Config.groovy:

    environments {
        test {
            plugin {
                excludes = "searchable"
            }
        }
    }
    

    【讨论】:

      【解决方案2】:

      我不确定如何禁用插件,native compass XML 可能有办法

      使用 grails,您也许可以通过以下方式使单元测试更容易接受…… 安装这个附加插件: grails install-searchable-config

      这将为您提供 grails-app/conf/Searchable.groovy 文件。你可以编辑 environment.test.searchable 闭包至少禁用 bulkIndexOnStartup 和 mirrorChanges。

       environments {
       test {
          searchable {
              // disable bulk index on startup
              bulkIndexOnStartup = false
              mirrorChanges = false
      
              // use faster in-memory index
              compassConnection = "ram://test-index"
          }
      }
       }
      

      【讨论】:

        【解决方案3】:

        要禁用运行测试使用的测试构建插件 - 如果您在 BuildConfig.groovy 中包含您的插件,则可以在其中执行以下操作;

        environments {
                    development {
                        compile ":searchable:0.6.6"
                    }
                    test {
                    }
                    production {
                        compile ":searchable:0.6.6"
                    }
                }
        }
        

        这会阻止构建环境在测试时包含插件,但是,如果您使用此环境构建 UAT 版本,这也会影响测试版本。

        【讨论】:

          猜你喜欢
          • 2015-05-31
          • 2011-12-14
          • 2015-06-18
          • 2012-07-28
          • 1970-01-01
          • 2012-08-12
          • 1970-01-01
          • 1970-01-01
          • 2012-01-28
          相关资源
          最近更新 更多