如何测试在异步测试中返回承诺的存根?(How to test a stub returning a promise in an async test?)
我如何以异步方式测试它?
it('Should test something.', function (done) { var req = someRequest, mock = sinon.mock(response), stub = sinon.stub(someObject, 'method'); // returns a promise stub.withArgs('foo').returns(Q.resolve(5)); mock.expects('bar').once().withArgs(200); request(req, response); mock.verify(); });
这里是测试的方法。
var request = function (req, response) { ... someObject.method(someParameter) .then(function () { res.send(200); }) .fail(function () { res.send(500); }); };
正如你所看到的,我正在使用node.js,Q(用于承诺),sinon用于嘲笑和存根以及mocha作为测试环境。 上面的测试由于请求方法的异步行为而失败,我不知道何时在测试中调用done()。
How can I test this in a async manner?
it('Should test something.', function (done) { var req = someRequest, mock = sinon.mock(response), stub = sinon.stub(someObject, 'method'); // returns a promise stub.withArgs('foo').returns(Q.resolve(5)); mock.expects('bar').once().withArgs(200); request(req, response); mock.verify(); });
And here is the method to test.
var request = function (req, response) { ... someObject.method(someParameter) .then(function () { res.send(200); }) .fail(function () { res.send(500); }); };
As you can see I am using node.js, Q (for the promise), sinon for mocking and stubbing and mocha as the test environment. The test above fails because of the async behaviour from the request method and I don't know when to call done() in the test.
原文:https://stackoverflow.com/questions/16015728
更新时间:2023-11-22 06:11
最满意答案
使用左外连接
SELECT p.name as professor, a.name as student, s_p.name as second_professor FROM users as p JOIN jobs ON(p.id = jobs.id_professor) JOIN users as s ON(jobs.id_student = a.id) left outer JOIN users as c ON(c.id = jobs.id_professor2) WHERE jobs.id = 2 ;
Use a left outer join
SELECT p.name as professor, a.name as student, s_p.name as second_professor FROM users as p JOIN jobs ON(p.id = jobs.id_professor) JOIN users as s ON(jobs.id_student = a.id) left outer JOIN users as c ON(c.id = jobs.id_professor2) WHERE jobs.id = 2 ;
相关问答
更多-
使用左外连接 SELECT p.name as professor, a.name as student, s_p.name as second_professor FROM users as p JOIN jobs ON(p.id = jobs.id_professor) JOIN users as s ON(jobs.id_student = a.id) left outer JOIN users as c ON(c.id = jobs.id_professor2) WHERE jobs.id = ...
-
你想要OUTPUT子句 UPDATE Items SET Clicks = Clicks + 1 OUTPUT INSERTED.Name WHERE Id = @Id You want the OUTPUT clause UPDATE Items SET Clicks = Clicks + 1 OUTPUT INSERTED.Name WHERE Id = @Id
-
在指定的列上使用NVL,同时仍然返回SQL中的所有其他列(Use NVL on a specified column while still returning all other columns in SQL)[2023-05-05]
没有办法选择*并中断*,因此您必须将每列或别名long / lat拼写为单独的名称并将它们选择两次。 SELECT A.* , case when Latitude is not null and Longitude is null then 0 else longitude end as LongitudeA , case when Latitude is null and Longitude is not null then 0 ... -
返回SQL结果集时是否可以创建一个新列?(Is it possible to create a new column when returning an SQL result set?)[2023-02-27]
是的,使用CASE表达式: SELECT ticket, CONVERT(Bit, CASE WHEN (TicketExpires < getDate()) THEN 0 ELSE 1 END) IsExpired FROM Authentication WHERE accountID = @id; 您将根据您是否在查找指示IsExpired或IsValid的值来更改返回值。 Yes, using the CASE expression: SELECT ticket, CON ... -
如果子查询按部门选择最大工资,则可以加入该结果以按部门获取列表。 SELECT firstname || ' ' || lastname AS "FULL NAME", E.department_id FROM EMPLOYEES E JOIN ( SELECT E.department_id, MAX(E.salary) as MAX_SALARY FROM EMPLOYEES E JOIN DEPARTMENTS D ON E.department_id = D.departm ...
-
根据sql中的第三个返回两列的所有组合(Returning all combinations of two columns based on a third one in sql)[2022-10-17]
此查询将为每个ID返回属性1和属性2之间的所有组合: SELECT A1.ID, attribute1, attribute2 FROM ( SELECT DISTINCT ID, Attribute1 FROM MyTable) A1 INNER JOIN ( SELECT DISTINCT ID, Attribute2 FROM MyTable) A2 ON A1.ID = A2.ID This query will return all combinations between at ... -
City不是FROM查询的一部分。 SELECT State,City, sum(possum) AS Total FROM (( SELECT SUM(table1.populationchange) AS popsum, table1.State as state, Table1.City as city FROM Table1 GROUP BY table1.State, table1.City) Union All ( SELECT SUM(table2. ...
-
SELECT *返回查询的所有表中的所有列。 这包括user_id列 - 一个来自表A,一个来自表B. 最佳做法是列出要特别返回的列名称,但缩短列表的另一个选项是: SELECT TableA.*, TableB.col1, TableB.col2, ...rest of B columns except user_id SELECT * returns all columns from all tables of the query. That inclu ...
-
日期数据类型没有时间。 问题是c#中DateTime的默认字符串表示形式。 尝试这个: Text1Box.Text = ((DateTime)cmd.ExcecuteScalar()).ToString("dd/MM/yyyy"); The date data type does not keep time. The problem is the default string representation of DateTime in c#. Try this: Text1Box.Text = ((Dat ...
-
您不是将列与当前时间戳(即NOW()的返回值)进行比较,而是与实际字符串'NOW()' 。 为了使用函数的结果,你必须删除引号。 You are not comparing the columns to the current timestamp (i.e. the return value of NOW()) but to the actual string 'NOW()'. In order to use the result of the function, you have to get rid o ...