我正在测试的代码如下所示:
def to_be_tested(x):
return round((x.a + x.b).c())
我想以x的形式传递一个Mock对象来测试它。我尝试这样做:
import unittest
import unittest.mock
class Test_X(unittest.TestCase):
def test_x(self):
m = unittest.mock.Mock()
to_be_tested(m)
# now check if the proper call has taken place on m
对x.a的调用
我正在努力学习如何使用Sinon、Mocha和柴进行JS单元测试。现在,我想确认我的一个函数实际上提出了一个api请求(所以我需要模拟我的函数)。我相信我已经在Sinon文档中找到了相关的模拟代码(它是用于测试Ajax的代码),但是,我无法理解发生了什么(文档非常稀少)。谁能解释it函数中3行代码的每一行(直接在下面)?
it("makes a GET request for todo items", function () {
sinon.replace(jQuery, "ajax", sinon.fake());
getTodos(42, sino
在用mock库编写Python时,我通常会得到这样的“调用方法的参数”,
from __future__ import print_function
import mock
m = mock.MagicMock(side_effect=lambda x: x * x)
m(4)
print("m called with: ", m.call_args_list)
(这将打印m called with: [call(4)])。问题:有任何方法获得返回值(在本例中是16)吗?
详细信息:在我的特定场景中,我想使用side_effect返回一个子模拟对象:内省这个对象,看看它上调
我正在尝试在现有的Web API项目中使用控制器上的nunit来创建单元测试用例。我使用Moq作为模拟框架。在这里,我想模拟存储库以返回一些预期的结果,我不确定在存储库模型设置和验证结果时我能做些什么。有没有人能建议我做得对不对?我刚刚拿了一个随机控制器来回答这个问题。对一些示例url的任何建议或指导都将非常感谢。
[TestCase]
public void TestMethod1()
{
AccountController ac = new AccountController();
var mockAuthRepository = new Mock<AuthRepos
假设我有下面的psuedo like测试代码:
//Let's import Mockito statically so that the code looks clearer
import static org.mockito.Mockito.*;
//mock creation
List mockedList = mock(List.class);
//using mock object
mockedList.add("one");
mockedList.clear();
//what do these two verify methods d
我很难尝试用外部依赖项对F#代码进行单元测试。
在C# (我的背景)中,通常会传入一个具有依赖项的类,然后再使用该类。为我的示例代码道歉,这是愚蠢的,但我只是想说明我的观点。
public class Foo {
IDependency d;
public Foo(IDependency d) { this.d = d; }
public int DoStuff(string bar) { return d.DoSomethingToStuff(bar); }
public int DoMoreStuff(string bar) {
int i = d.DoS
interface ITaxi
{
int Fare { get; set; }
int getTotalFare();
}
class Taxi : Car, ITaxi
{
public Taxi(Engine e) : base (e){ }
public int Fare { get; set; }
public int getTotalFare()
{
return 0;
}
}
[TestFixture]
class TestTaxi
{
[Test]
我对Ruby和Rspec有相当广泛的背景。然而,当我学习Node和Mocha时,我遇到了我似乎无法理解的语法。
以为例,测试路由包括以下内容(使用CoffeeScript)。还应该注意到,我已经看到了下面的例子,许多其他地方向初学者解释摩卡。
require "should"
routes = require "../routes/index"
describe "routes", ->
describe "index", ->
it "should display index with po
假设我有下面的代码,它接受一个距离函数和两个点,并输出这个距离:
def distance(dist_fun, p1, p2):
## SOME ASSERTION HERE on dist_fun ##
## Suppose, just for this post, we've already checked p1 and p2 are
## n-tuples of integers.
assert (len(p1) == len(p2)), "Coordinates are different dimensions."