函数返回正确结果时,Jasmine测试不通过

时间:2015-07-08 05:20:56

标签: javascript jasmine karma-jasmine

有一个返回值的简单函数:

<Style TargetType="{x:Type TabItem}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type TabItem}">
                    <Border>
                    <Grid Margin="0,0,20,0" Height="40">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="24"/>
                            <ColumnDefinition Width="150"/>
                            <ColumnDefinition Width="24"/>

                        </Grid.ColumnDefinitions>
                        <Border
     x:Name="Border"
     Grid.Column="1"
     CornerRadius="0,0,0,0"    Margin="-2,0,0,0"
     Background="white" RenderTransformOrigin="0.5,0.5" >
                            <Border.RenderTransform>
                                <TransformGroup>
                                    <ScaleTransform/>
                                    <SkewTransform/>
                                    <RotateTransform Angle="180"/>
                                    <TranslateTransform/>
                                </TransformGroup>
                            </Border.RenderTransform>
                        </Border>
                        <Path
     x:Name="RoundedCorner"
     Grid.Column="0"
     Stretch="Fill"
     Data="M9.5833315,2.85 C7.8503315,1.223 2.509,0 0,0 0,0 0,19 0,19 L33.541661,19.000001 24.916663,18.050001 20.224135,15.560345 17.249997,12.35 z"
     Fill="white" RenderTransformOrigin="0.5,0.5" Grid.ColumnSpan="2" Margin="0,0,139,0" >
                            <Path.RenderTransform>
                                <TransformGroup>
                                    <ScaleTransform/>
                                    <SkewTransform/>
                                    <RotateTransform Angle="180"/>
                                    <TranslateTransform X="-11.000000000000014"/>
                                </TransformGroup>
                            </Path.RenderTransform>
                        </Path>




     <Path
     x:Name="RoundedCorner1"
     Grid.Column="2"
     Stretch="Fill"
     Data="M9.5833315,2.85 C7.8503315,1.223 2.509,0 0,0 0,0 0,19 0,19 L33.541661,19.000001 24.916663,18.050001 20.224135,15.560345 17.249997,12.35 z"
     Fill="white" RenderTransformOrigin="0.5,0.5" Margin="-1,0,-9,-1" >
                            <Path.RenderTransform>
                                <TransformGroup>
                                    <ScaleTransform ScaleY="1" ScaleX="-1"/>
                                    <SkewTransform AngleY="0" AngleX="0"/>
                                    <RotateTransform Angle="180"/>
                                    <TranslateTransform Y="-1" X="0"/>
                                </TransformGroup>
                            </Path.RenderTransform>
                        </Path>

                        <ContentPresenter               
     x:Name="ContentSite"
     Panel.ZIndex="20" Grid.Column="1"
     VerticalAlignment="Center"
     HorizontalAlignment="Center"
     ContentSource="Header"
     Margin="12,2,12,2"
     RecognizesAccessKey="True"/>
                    </Grid>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsSelected" Value="True">
                            <Setter Property="Panel.ZIndex" Value="100" />
                            <Setter TargetName="Border" Property="Background" Value="#FF333333" />
                            <Setter TargetName="RoundedCorner" Property="Fill" Value="#FF333333" />
                            <Setter TargetName="RoundedCorner1" Property="Fill" Value="#FF333333" />
                            <Setter TargetName="ContentSite" Property="TextElement.Foreground" Value="White" />
                    <!--        <Setter TargetName="CloseButton" Property="Visibility" Value="Visible" />-->
                        </Trigger>
                        <Trigger Property="IsEnabled" Value="False">
                            <Setter TargetName="Border" Property="Background" Value="white" />
                            <Setter TargetName="RoundedCorner" Property="Fill" Value="white" />
                            <Setter TargetName="RoundedCorner1" Property="Fill" Value="white" />
                           <Setter TargetName="ContentSite" Property="TextElement.Foreground" Value="Black" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>

    </Style>

运行我的测试:

obj.id = function(id) {
    if (!id) {
        throw new Error('obj.id: no param');
    } else if (typeof(id) === 'string') {
        return obj.doc.getElementById(id);
    } else {
        return null;
    }
};

测试未能通过,即使它应该是通过。

it('Should return null if passed parameter is not a string', function() {
    expect(function() {
        obj.id({});
    }).toBeNull();
});

这个过去了。

有什么想法吗?

2 个答案:

答案 0 :(得分:0)

您将{}作为变量传递给id函数。即使对象没有键,在true条件下使用时,空对象的计算结果为if

您可以使用以下内容证明这一点:

({}) ? console.log('not falsey') : console.log('falsey');
// -> outputs 'not falsey'

也许您可以使用hasOwnProperty()功能代替?

({}.hasOwnProperty()) ? console.log('not empty') : console.log('empty');
// -> outputs 'empty'

答案 1 :(得分:0)

使用以下

修复此问题
expect(wh.id({})).to.be.null

根据需要通过测试。