Grails String.encodeAsBase64()在Spock测试中失败

时间:2015-06-13 04:56:29

标签: unit-testing grails spock

当我在Grails中运行它时,此代码可以正常工作。

String getLoginToken() {
    generatePassword()
    passwordExpired = false
    [email, password].join(',').encodeAsBase64()
}

然而,这个Spock测试失败

def "test getLoginToken"() {
    setup:
    String email = "bacon@eggs.edu"
    Person person = new Person(email: email)

    when:
    String token = person.getLoginToken()

    then:
    token.decodeBase64() == "$email,$person.password"
}

有以下异常

| Failure:  test getLoginToken(com.campuscardtools.myphotoid.PersonSpec)
|  groovy.lang.MissingMethodException: No signature of method: java.lang.String.encodeAsBase64() is applicable for argument types: () values: []
Possible solutions: decodeBase64()
    at com.campuscardtools.myphotoid.Person$$EPFScS6i.getLoginToken(Person.groovy:140)
    at com.campuscardtools.myphotoid.PersonSpec.test getLoginToken(PersonSpec.groovy:68)

我的理解是Groovy在String类上提供了encodeAsBase64()(参见:http://mrhaki.blogspot.com/2009/11/groovy-goodness-base64-encoding.html),那么为什么这不能在单元测试中工作呢?

3 个答案:

答案 0 :(得分:2)

而不是

Thank you all for the help, this is a great community!
So, i used a converter as maxime-tremblay-savard suggested.



      class GetCalleridConverter : IValueConverter
            {
                    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
                {
                string callerId = (string)value;
                Regex angleBrackets = new Regex("<[^(>)]*>");
                Regex quotes = new Regex("\"([^\"]*)\"");
                if (parameter.ToString() == "Number")
                    {
                    //your regex to return what's in the double quotes ""

                    Match angled = angleBrackets.Match(callerId);
                    return angled.ToString();
                    }
                else  
                    {
                    Match quot = quotes.Match(callerId);
                    return quot.ToString();
                    }


                }

//And then i binded the item to xaml

.....
  <Page.Resources>
        <valueconverter:GetCalleridConverter x:Key="GetCallerIdConverter" />
    </Page.Resources>
.......
 <GridViewColumn Header="Caller Name" Width="auto" DisplayMemberBinding="{Binding Path=clid, Converter={StaticResource GetCallerIdConverter}, ConverterParameter='Name'}"/>
                    <GridViewColumn Header="Caller Number" Width="auto" DisplayMemberBinding="{Binding Path=clid, Converter={StaticResource GetCallerIdConverter}, ConverterParameter='Number'}"/>

//So, i got two columns with caller Name and Caller Number, only thing left to remove quotes and angle brackets....

你需要

"Blah".encodeAsBase64()

没有'As'

答案 1 :(得分:1)

您还可以为您正在使用的方法添加mockCodec。

and: "add the Base64 codec"
    mockCodec(org.codehaus.groovy.grails.plugins.codecs.Base64Codec)

答案 2 :(得分:0)

这样可行,但我摔倒了就像一个糟糕的黑客。当然必须有更清洁的解决方案:

def cleanup () {
    String.metaClass = null
}

def "test getLoginToken"() {
    setup:
    String email = "bacon@eggs.edu"
    Person person = new Person(email: email)
    String encoded = null

    and:
    String.metaClass.encodeAsBase64 {
        encoded = delegate
        return delegate
    }

    when:
    String token = person.getLoginToken()

    then:
    token == "$email,$person.password"
    encoded == "$email,$person.password"
}