English 中文(简体)
我怎么能够 window窗。 DOM-centric 简讯
原标题:How can I mock window.document for testing DOM-centric JavaScript?

我正试图撰写一些称作窗口的测试。 文件,我想冲淡实际呼吁本身,因此我可以无动于衷。 下面的法典赢得了工作:

window = {"document": ""};
document = window.document;
document.cookie = "";
document.location = {"hostname": "test.myserver.com"}

我有以下错误:

TypeError: Cannot set property window that has only a getter. in file:...

是否有任何想法来弄清这一点?

我正在使用Jasmine和jasmine-maven-plugin,这样会有任何变化。

问题回答

如果你必须在浏览器中操作该守则,你可在<条码/代码>的说明中总结一下你的全部代码:

with ({window: {}}) {
     ...
}

如果你修改了你的法典,以在任何地方打赢窗户,那么就使用了什么。 然后,您可使用<代码>var.2/ = 窗口;,如不测试和var.2/ ={”文件:“}; 测试时。

如果你在以浏览器为基础的某种情况下重新这样做,你就能够用窗户书写。 你们能否通过使用习惯变量而不是窗口来进行测试?

如果你能够把你的所有法典放到单一档案中(例如,有一份叫作“编目”的手稿),就可以做到:

window.realWindow = window;

(function(){

var window = {document: {something: "hi!"}};
var document = window.document;

///////////////////////////////////
// your code goes here, for example:

function test (foo) {
 alert (document.something + " " + foo);
 realWindow.document.title = foo;
 }

test("from inside");

// to make the function "test" reachable from the outside
realWindow.global_test = test;

///////////////////////////////////

})();

global_test("from outside");

如今,你们的全球已经取得了真正的胜利,但“窗口”可以从任何地方获得,并且将成为你们自己的版本。 请注意,这将打破一些构造,使“从外部”做事更加困难。

Edit: 举例说明如何从封闭功能之外获取东西





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签