小编Bra*_*ite的帖子

使用Sinon-Chai时,测试失败显示"错误:超过2000ms超时"

我有以下路由(快速),我正在编写集成测试.

这是代码:

var q = require("q"),
    request = require("request");

/*
    Example of service wrapper that makes HTTP request.
*/
function getProducts() {

    var deferred = q.defer();

    request.get({uri : "http://localhost/some-service" }, function (e, r, body) {
        deferred.resolve(JSON.parse(body));
    });

    return deferred.promise;
}

/*
    The route
*/
exports.getProducts = function (request, response) {
    getProducts()
        .then(function (data) {
            response.write(JSON.stringify(data));
            response.end();
        });
};
Run Code Online (Sandbox Code Playgroud)

我想测试所有组件一起工作,但是假HTTP响应,所以我正在为请求/ http交互创建一个存根.

我正在使用Chai,Sinon和Sinon-Chai以及Mocha作为试车手.

这是测试代码:

var chai = require("chai"),
    should = chai.should(),
    sinon = require("sinon"),
    sinonChai = require("sinon-chai"),
    route = require("../routes"),
    request = …
Run Code Online (Sandbox Code Playgroud)

unit-testing mocha.js node.js sinon chai

9
推荐指数
1
解决办法
2万
查看次数

在Internet Explorer中意外调用JQuery UI Autocomplete focus()

我使用JQuery UI创建了一个简单的自动完成控件.我有一个输入字段的默认值,显示"输入您的关键字...".我设置了一个focus()事件,当用户将焦点设置为输入字段进行输入时,该事件将清除输入.

在IE中,当您键入并且菜单显示项目列表时,从菜单项中选择项目时,再次调用focus().这个对focus()的额外调用只发生在IE中.副作用是从文本字段中清除所选菜单项.

通过利用这个autocomplete.focus()事件,我有一个非常原始的解决方案.当用户将鼠标悬停在选定的菜单项上时会触发此事件.在这里,我可以设置一个全局布尔变量,可以用来告诉输入字段上的focus()事件,菜单项是活动的/可见的,因此不清除输入值.当然,这是一个黑客!

这个问题有替代方案(不那么hacky!)吗?

这是代码:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Autocomplete demo</title>
    <link type="text/css" href="css/ui-lightness/jquery-ui-1.8.18.custom.css" rel="stylesheet" />
    <script type="text/javascript" src="js/jquery-1.7.1.min.js"></script>
    <script type="text/javascript" src="js/jquery-ui-1.8.18.custom.min.js"></script>
</head>
<body>
<script>
    $(function () {
        var availableTags = [
            "ActionScript",
            "AppleScript",
            "Asp",
            "BASIC",
            "C",
            "C++",
            "Clojure",
            "COBOL",
            "ColdFusion",
            "Erlang",
            "Fortran",
            "Groovy",
            "Haskell",
            "Java",
            "JavaScript",
            "Lisp",
            "Perl",
            "PHP",
            "Python",
            "Ruby",
            "Scala",
            "Scheme"
        ];
        $("#tags").autocomplete({
            source: availableTags,
            focus: function (event, ui) {
               // This event fires when an item from the …
Run Code Online (Sandbox Code Playgroud)

jquery-ui

5
推荐指数
1
解决办法
4708
查看次数

验证域实体中的唯一值

我有一个场景,需要验证域实体属性的唯一性,然后才能将其保存到数据库。这是一个简单的Product类。假设我想验证在创建新产品时ProductKey 字符串属性是唯一的

public class Product : EntityBase
{
    int ID { get; set; }
    string ProductKey { get; set; }
    int CategoryID { get; set; }

    bool IsValid
    {
        get
        {
            if (string.IsNullOrEmpty(ProductKey))
            {
                ValidationErrors.Add("ProductKey Required.");
            }

            if (CategoryID == 0)
            {
                ValidationErrors.Add("CategoryID Required.");
            }

            /* Validation that the product key is unique could go here? i.e. requires a database read. */

            return ValidationErrors.Count() == 0;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

由于我使用领域驱动设计,产品实体不了解持久性或服务层。我可以向 Service 方法添加一个检查,如下所示:

public class ProductService 
{ …
Run Code Online (Sandbox Code Playgroud)

c# design-patterns domain-driven-design

1
推荐指数
1
解决办法
1583
查看次数