Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在Jasmine中加载JS源文件之前加载助手_Javascript_Jquery_Ruby On Rails_Jasmine_Gon - Fatal编程技术网

Javascript 如何在Jasmine中加载JS源文件之前加载助手

Javascript 如何在Jasmine中加载JS源文件之前加载助手,javascript,jquery,ruby-on-rails,jasmine,gon,Javascript,Jquery,Ruby On Rails,Jasmine,Gon,这个问题特别是因为我使用的是gon,但从概念上讲,它可以应用于其他情况,所以我尝试将其扩展一点。Gon是一个漂亮的gem,它允许您在Rails控制器中声明一个变量,然后在Javascript中使用它。例如: def index gon.this = "that" end // index.html <script> console.log(gon.this) // would print "that" </script> 如您所见,gon立即被使用!我的问

这个问题特别是因为我使用的是
gon
,但从概念上讲,它可以应用于其他情况,所以我尝试将其扩展一点。Gon是一个漂亮的gem,它允许您在Rails控制器中声明一个变量,然后在Javascript中使用它。例如:

def index
  gon.this = "that"
end

// index.html
<script>
  console.log(gon.this)
  // would print "that"
</script>
如您所见,
gon
立即被使用!我的问题是Jasmine一直说,
gon
还没有定义,所以我不知道如何以语义上合理的方式“注入”它。我能想到的唯一方法是创建一个假的
.js
文件,并在加载正在测试的文件之前要求它作为源文件存在于我的
javascript.yml
中。也就是说,它看起来是这样的:

// test.js
function loadProperPage() {
  if (gon.this == "that") {
    ...
  } else {
    ...
  }
}
src_files:
  - assets/gon_variables.js
  - assets/test.js
但这不是一个可持续的解决方案,因为这样我就不能为不同的规范更改
gon_variables.js
文件中的虚拟变量

最后一句话的意思是,我一直在排除故障,并发现在fixture或spec文件本身中声明
gon
变量将不起作用。也就是说,以下两种方法都不能解决问题

/////// spec file approach

//spec file

describe...
  beforeEach(function() {
    gon.this = "that"
  })
})

/////// fixture file approach

// spec file
describe...
  beforeEach(function() {
    loadFixtures('test.html')
  })
})

// test.html fixture
<head>
   <script>
     var gon = {permission: "new"}
   </script>
</head>
<body>
  ...
</body>
//spec文件方法
//规格文件
描述。。。
beforeach(函数(){
gon.this=“that”
})
})
///////夹具锉刀法
//规格文件
描述。。。
beforeach(函数(){
LoadFixture('test.html')
})
})
//test.html夹具
var gon={permission:“new”}
...

这让我回到我的问题上来。我的调试显然表明,
jasmine.yml
中的JS文件在fixture或spec文件(及其在操作之前)之前加载。因此,在
jasmine.yml
中的JS文件之前,如何加载动态代码?是否有我可以使用的助手或?请记住,这一点的全部要点是,我希望根据规范更改在JS文件之前加载的代码。例如,有时我希望有
gon.this=“that”
,但其他时候我希望
gon.this=“thas”

您尝试测试的函数loadProperPage,是否在文档就绪块中?比如,您是否使用类似于
$(function(){loadProperPage()})的方法调用它?是的,我有几个函数使用
gon
,其中一些函数是在document.ready上调用的。其他函数只在按下按钮时调用,但前者是问题所在
/////// spec file approach

//spec file

describe...
  beforeEach(function() {
    gon.this = "that"
  })
})

/////// fixture file approach

// spec file
describe...
  beforeEach(function() {
    loadFixtures('test.html')
  })
})

// test.html fixture
<head>
   <script>
     var gon = {permission: "new"}
   </script>
</head>
<body>
  ...
</body>