我试图测试一个调用window.location.search的简单函数.我正在尝试了解如何存根此调用,以便我可以返回我选择的网址.
功能:
getParameterByName: (name) =>
name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]")
regexS = "[\\?&]" + name + "=([^&#]*)"
regex = new RegExp(regexS)
results = regex.exec(window.location.search) //Stub call to window.location.search
if(results == null)
return ""
else
return decodeURIComponent(results[1].replace(/\+/g, " "))
Run Code Online (Sandbox Code Playgroud)
测试用例:
describe "Data tests", () ->
it "Should parse parameter from url", () ->
data = new Data()
console.log("search string: " + window.location.search) //prints "search string:"
window.location.search = "myUrl"
console.log("search string: " + window.location.search) //prints "search string:"
console.log(data.getParameterByName('varName'))
expect(true).toBe(true)
Run Code Online (Sandbox Code Playgroud)
我最初的尝试是直接返回一个值:
sinon.stub(window.location.search).returns("myUrl")
Run Code Online (Sandbox Code Playgroud)
当然,这不起作用.我认为我没有正确指定存根,但它显示了我的意图. …
我试图在我的单元测试中测试抛出异常.我尝试了删除方法的metaclass,但它不想坚持.你能从代码中看出我做错了什么吗?
单元测试代码:
@TestFor(ProductController)
@TestMixin(DomainClassUnitTestMixin)
class ProductControllerTests {
void testDeleteWithException() {
mockDomain(Product, [[id: 1, name: "Test Product"]])
Product.metaClass.delete = {-> throw new DataIntegrityViolationException("I'm an exception")}
controller.delete(1)
assertEquals(view, '/show/edit')
}
Run Code Online (Sandbox Code Playgroud)
控制器动作代码:
def delete(Long id) {
def productInstance = Product.get(id)
if (!productInstance) {
flash.message = message(code: 'default.not.found.message', args: [message(code: 'product.label', default: 'Product'), id])
redirect(action: "list")
return
}
try {
productInstance.delete(flush: true)
flash.message = message(code: 'default.deleted.message', args: [message(code: 'product.label', default: 'Product'), id])
redirect(action: "list")
}
catch (DataIntegrityViolationException e) {
flash.message = message(code: 'default.not.deleted.message', args: …Run Code Online (Sandbox Code Playgroud) 遵循托德·莫托(Todd Motto)的优秀指南之一(https://toddmotto.com/dynamic-page-titles-angular-2-router-events),我遇到了一种我无法解释的情况。
这是一些代码:
this.router.events
.filter((event) => event instanceof NavigationStart)
.map(() => this.activatedRoute)
.map((route) => {
console.log('route pre-while: ', route); // Shows ActivatedRoute object
console.log('pre-while route child: ', route.firstChild); // null
while (route.firstChild) {
console.log('while route: ', route);
route = route.firstChild;
}
console.log('post while route: ', route);
return route;
})
.subscribe((elem) => {
console.log('elem: ', elem);
});
Run Code Online (Sandbox Code Playgroud)
在此代码块中,“route pre-while”日志语句在 Chrome 控制台中显示一个 ActivatedRoute 对象。如果我在 Chrome 的控制台窗口中展开该对象,我可以单击该firstChild属性并看到另一个ActivatedRoute对象,这正如我所期望的那样。然而,问题是,第二条日志语句“pre-while route child”输出null到控制台。此外,WHILE 循环永远不会执行,因为route.firstChild计算结果为null。谁能给我解释一下这是为什么???为什么我可以记录该对象并看到 a …
我有一个我启动时使用的Gradle应用程序./gradlew run.这工作正常,但我正在尝试部署到AWS实例(Ubuntu 12),我希望脚本在启动时执行.我尝试使用上面的命令编写一个startup.sh文件,但没有骰子.我也尝试将命令添加到/etc/rc.local文件中,但这似乎也不起作用.有人可以告诉我如何在启动时执行`./gradlew run'吗?谢谢!