2016-11-24 79 views
2

我在测试中复制SVG功能时遇到了问题。 Jasmine与Sinatra网络应用程序中的jasmine-jquery一起成功连线。测试是针对与SVG对象关联的JavaScript函数的。如:在Jasmine中测试SVG对象时遇到问题

function findMapImage() { 
    return $("#svg")[0].contentDocument; 
} 

function findRegions(map) { 
    return map.querySelectorAll('path'); 
} 

而且茉莉花灯具看起来是这样的(我已经剥离了一些细节出来):

<object id="svg" type="image/svg+xml"> 
    <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 484.33 547" height="500"> 
    <path/> 
    <path/> 
    </svg> 
</object> 

我已经添加了以下几行到我的测试:

var map = findMapImage(); 
console.log(map) 

而且收益率的console.log:

#document 
    <html> 
    #shadow-root (open) 
     <shadow> 
     <head> 
     <body> 
     </shadow> 
    <head></head> 
    <body></body> 
    <html> 

但我应该看到的是更多的东西一样:

#document 
    <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 484.33 547" height="500"> 
    <defs>...</defs> 
    <title>...</title> 
    <path></path> 
    etc. 
    </svg> 

我已经read那的xmlns'需要资源类型的SVG对象的图像/ SVG + XML“。所以我最好的猜测是xmlns资源没有加载。所以,也许我需要弄清楚如何加载远程资源或在本地下载它?我愿意接受其他想法。

+0

难道适合您的用例,避免了''元素,取而代之的是''直接在HTML中使用? – RJHunter

+0

看不到为什么不。只是试了一下,并在标签内移动了id“svg”。它产量不确定。 – Zollie

+0

使用普通内联''标记,可以从'findMapImage'中删除'.contentDocument',并直接使用该元素。 – RJHunter

回答

0

比方说,你的SVG已经宽度和高度500px的话,就可以:

describe('the svg' ,function() { 

    it('should be created', function() { 
     expect(findMapImage()).not.toBeNull(); 
    }); 

    it('should have the correct height', function() { 
    expect(findMapImage().attr('height')).toBe('500'); 
    }); 

    // you can use this if you have width, just in case 
    it('should have the correct width', function() { 
     expect(findMapImage().attr('width')).toBe('500'); 
    }); 

    function findMapImage() { 
     return $("#svg")[0].contentDocument; 
    } 

}); 
+0

嘿,谢谢你的回答,但这不是我所问的。我知道如何写一个'它块'。但我实际上并没有得到一个SVG的工作。我添加了一个编辑来强调问题。 – Zollie

+0

如果你'console.log($(“#svg”))',你会得到什么? –

+0

它返回[函数]和$(“#svg”)[0]返回函数anonymous()。我担心的是我们正在改变我的功能而不是测试它。 – Zollie

相关问题