我正在开发一个大量使用JavaScript的应用程序.我需要对这段代码进行单元测试.为了做到这一点,我依靠Jasmine.
我的一些JavaScript代码会抛出JavaScript Error对象.这些对象将值分配给Error对象的message和name属性.我为name属性分配了一种异常.例如,有时名称设置为"OutOfRangeException",有时设置为"ArgumentException"等.
如何在Jasmine框架中使用toThrowError函数来测试抛出的错误是否具有特定名称?目前,我的JavaScript如下所示:
function getRandomNumber(max) {
if ((!isNaN(parseFloat(max)) && isFinite(max)) === false) {
var error = new Error('You must provide a number');
error.name = 'ArgumentException';
throw error;
}
if ((max === null) || (max < 1) || (max > 100)) {
var error = new Error('The maximum value must be greater than 0 and less than 100.');
error.name = 'ArgumentOutOfRangeException';
throw error;
}
return Math.floor(Math.random() * max) + 1;
}
function ArgumentException(message) {
this.name = 'ArgumentException';
this.message = message …Run Code Online (Sandbox Code Playgroud) 我有一个ASP.NET MVC网站。该网站的站点地图如下所示:
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>http://www.mysite.com/contact</loc>
<lastmod>2013-06-04</lastmod>
<changefreq>never</changefreq>
</url>
<url>
<loc>http://www.mysite.com/contact-us</loc>
<lastmod>2013-06-04</lastmod>
<changefreq>never</changefreq>
</url>
<url>
<loc>http://www.mysite.com/about/books</loc>
<lastmod>2013-06-18</lastmod>
<changefreq>monthly</changefreq>
</url>
<url>
<loc>http://www.mysite.com/about/blog</loc>
<lastmod>2012-05-02</lastmod>
<changefreq>never</changefreq>
</url>
<url>
<loc>http://www.mysite.com/about/blog/post-1</loc>
<lastmod>2012-05-02</lastmod>
<changefreq>never</changefreq>
</url>
<url>
<loc>http://www.mysite.com/about/blog/post-2</loc>
<lastmod>2012-02-15</lastmod>
<changefreq>never</changefreq>
</url>
</urlset>
Run Code Online (Sandbox Code Playgroud)
我试图弄清楚如何使用C#中的Linq-to-XML查询此站点地图。我正在尝试编写一个仅返回博客文章条目的查询。博客文章条目的loc属性值以http://www.mysite.com/about/blog/开头。目前,我正在成功加载和查询站点地图。但是,我无法弄清楚如何仅过滤博客文章,然后按lastmod值排序。这是我到目前为止的内容:
XDocument sitemap = XDocument.Load(Server.MapPath("/resources/sitemap.xml"));
IEnumerable<XElement> blogs = from post in sitemap.Descendants("url")
select post;
Run Code Online (Sandbox Code Playgroud)
如何只过滤我的博客文章?我什至只查询网址似乎都行不通。