小编Old*_*Fox的帖子

EXPECT_CALL的对话是什么?

我有一些期望 EXPECT_CALL (...)

EXPECT_CALL(t1, foo()).Times(1);
Run Code Online (Sandbox Code Playgroud)

我想创造对面.我希望不会执行某个功能.

我应该使用什么方法?有点像EXPECT_NOT_CALL (...)

unit-testing assert mocking googletest gmock

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

对于类似的自定义类列表,NUnit的CollectionAssert返回false

这是我的班级:

public class MyClass
{
    public string Name { get; set; }
    public string FaminlyName { get; set; }
    public int Phone { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

然后我有两个相似的列表:

List<MyClass> list1 = new List<MyClass>()
{
    new MyClass() {FaminlyName = "Smith", Name = "Arya", Phone = 0123},
    new MyClass() {FaminlyName = "Jahani", Name = "Shad", Phone = 0123}
};
List<MyClass> list2 = new List<MyClass>()
{
    new MyClass() {FaminlyName = "Smith", Name = "Arya", Phone = 0123},
    new MyClass() {FaminlyName = "Jahani", Name …
Run Code Online (Sandbox Code Playgroud)

c# nunit assert equality

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

Moq:Mock SetUp方法仅在测试期间返回null

我刚刚将依赖注入添加到我的应用程序中,并希望对我的方法运行模拟测试以确保它们正常工作.

我有一个Person具有以下属性的域类:

public class Person : DomainBase
{
   public string FirstName { get; set; }
    public string LastName { get; set; }
    public char Gender { get; set; }
    public DateTime DOB {get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我有一个manager与其他域类型管理员共享的部分类:

public partial class Manager : IManager1
{
    private IHiveTiesContext _ctx; 
    public Manager(IHiveTiesContext context)
    {
        _ctx = context;
    }
}
Run Code Online (Sandbox Code Playgroud)

界面IManager1是从我的PersonManager类中提取的,包含所有需要的方法.

我的PersonManager CreatePerson()方法正在被调用和测试:

public partial class Manager : IManager1
{
    public Person CreatePerson(string fn, …
Run Code Online (Sandbox Code Playgroud)

c# unit-testing entity-framework moq mocking

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

单元测试使用外部dll的方法

我有一个名为A的项目,它有一个名为ClassA的类. ClassA有一个名为ReadBlock()的方法,它创建一个CloudBlockBlob对象并调用其中一个方法.

CloudBlockBlob是一个位于Microsoft.WindowsAzure.Szrage.Blob命名空间的类,该命名空间位于Microsoft.WindowsAzure.Storage.dll中.

我的项目A有一个名为A.Tests的单元测试项目.现在,我想测试方法ReadBlock().为了测试它,我需要模拟CloudBlockBlob对象并拦截对其方法的调用,返回自定义值并验证方法是否被调用.

  • 如何模拟在方法内完全创建的对象?
  • 我可以以某种方式更改项目A的DLL引用并将其引用到创建模拟对象而不是真实对象的模拟dll吗?
  • 我可以在A.Tests类中使用我自己的实现覆盖项目AMicrosoft.WindowsAzure.Storage.Blob中的类的调用吗?

更新: 问题是我是否可以在不修改项目A的代码的情况下完成此操作.

谢谢!

c# unit-testing moq mocking azure-storage-blobs

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

如何使用反射对Dispose()进行单元测试?

我想为实现IDisposable的类编写单元测试.该类有许多私有字段,也实现IDisposable.在我的测试中,我想验证当我调用时Dispose(),它正确调用Dispose()其所有IDisposable字段.基本上,我希望我的单元测试看起来像这样:

var o = new ObjectUnderTest();
o.Dispose();
Assert.IsFalse(ObjectHasUndisposedDisposables(o));
Run Code Online (Sandbox Code Playgroud)

我正在考虑使用反射来实现这一目标.看起来这是一个相当普遍的要求,但我找不到它的任何例子.

有人试过吗?

编辑 - 我不想将Disposables注入被测试的类中.

c# reflection unit-testing assert dispose

3
推荐指数
2
解决办法
2553
查看次数

如何在不添加参数的情况下对基于时间的功能进

我创建了一个函数,它返回剩下的秒数直到下一次出现,但是我遇到了为它编写单元测试的问题.人们如何测试这种有调用功能的功能datetime.now()

添加另一个参数(current_time)似乎是错误的,只是为了测试它,因为它改变了函数的初始要求.

功能测试是.

from datetime import datetime, time, timedelta

def get_time_left(target_time):
    '''return float of number of seconds left until the target_time'''

    if not isinstance( target_time, time ):
        raise TypeError("target_time must be datetime.time")

    curr_time = datetime.now()
    target_datetime = datetime.combine( datetime.today(), target_time )
    if curr_time > target_datetime:
        target_datetime = curr_time + timedelta(1)

    seconds_left = (curr_time - target_datetime).total_seconds()

    return seconds_left
Run Code Online (Sandbox Code Playgroud)

对它的测试是.

class TestDTime(unittest.TestCase):

    def test_time_left(self):
        dt_now = datetime.now()
        tm_5sec_future = ( dt_now + timedelta(0,5) ).time()
        self.assertEqual( dtime.get_time_left(tm_5sec_future), 5.0)
Run Code Online (Sandbox Code Playgroud)

结果是. …

python datetime unit-testing assert mocking

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

如何使用C#获取当前位置的经度和纬度

我是C#的新手,我已经尝试获取当前位置的纬度和经度。以下是我尝试的代码。

public string GetLocationProperty()
{
    double a = 0.0;
    string b = "";
    GeoCoordinateWatcher watcher = new GeoCoordinateWatcher();

    // Do not suppress prompt, and wait 1000 milliseconds to start.
    watcher.TryStart(false, TimeSpan.FromMilliseconds(1000));

    GeoCoordinate coord = watcher.Position.Location;

    if (coord.IsUnknown != true)
    {
        //Console.WriteLine("Lat: {0}, Long: {1}",
        //    coord.Latitude,
        //    coord.Longitude);
        a = coord.Latitude;
        b = a.ToString();
    }
    else
    {
         Console.WriteLine("Unknown latitude and longitude.");
    }
    return b;
}
Run Code Online (Sandbox Code Playgroud)

c#

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

如何在C#中单元测试Textrenderer.DrawText方法

public static void DrawText(IDeviceContext dc, string text, Font font, Point pt, Color foreColor, TextFormatFlags flags);
Run Code Online (Sandbox Code Playgroud)

我有一个测试器应用程序,用于我的ExtendedComboBox.以下代码中给出的所有String项目都存在于我的测试器应用程序中的ComboBox项目中.如何对上述方法进行单元测试,因为它返回void?测试TextRenderer.Drawtext的另一种方法是什么?是否有任何替代测试OnDrawItem方法来绘制ComboBox文本.

[TestMethod()]
public void ExtendedComboBoxOnDrawPrefixTest()
{
    ExtendedComboBox cboTest = new ExtendedComboBox ();

    // List of strings having special characters.

    string[] items = { 
        "&One",
        "T&wo",
        "E!xclamation",
        "Am@persat",
        "H#ash",
        "Dollar$Sign",
        "Perc%ent",
        "Ci^rcumflex",
        "Ast*erisk",
        "Hy-phen",
        "Und_erscore",
        "pl+us",
        "Equ=als",
        "Col:on",
        "Semi;colon",
        "Co'mma",
        "Inverted\"Comma",
        "Apos'trophe",
        "Pip|e",
        "Open{Brace",
        "Close}Brace",
        "OpenBr[acket",
        "CloseBr]acket",
        "BackS\\lash",
        "ForwardSl/ash",
        "LessT<han",
        "Greate>rThan",
        "Questio?nMark",
        "Do.t",
        "Three",
        "Four",
        "Five",
        "Six",
        "This is a really extremely long string value for …
Run Code Online (Sandbox Code Playgroud)

c# unit-testing

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

如何使用 NUnit 和 Rhino Mocks 模拟 HttpContext.Current.Items

我在 (WebApi) 项目上使用NUnitRhinoMocks进行单元测试。

有一种方法我正在尝试为其编写测试,它应该向 HttpContext.Current.Items 添加一个项目。

public override void OnActionExecuting(HttpActionContext actionContext)
{
    HttpContext.Current.Items.Add("RequestGUID", Guid.NewGuid());
    base.OnActionExecuting(actionContext);
}
Run Code Online (Sandbox Code Playgroud)

我不知道HttpContext.Current.Items从测试方法中运行时如何使该方法可用。我怎样才能做到这一点?

另外,我如何检查项目是否已添加(我可以/应该使用什么样的断言)

c# nunit unit-testing rhino-mocks mocking

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

如何为单元测试提供HttpWebResponse.GetResponseAsync()?

我被指派为我无权修改的应用程序编写单元测试.我想要单元测试的方法进行如下调用:

HttpWebResponse response = await request.GetResponseAsync() as HttpWebResponse;
Run Code Online (Sandbox Code Playgroud)

我在这个项目中使用过Microsoft Fakes进行了数以千计的其他测试,所以我想我会在这里做同样的事情.在我看来,最简单,最干净的解决方案是使用该request.GetResponseAsync()方法.然后我可以返回一些虚假内容并确保该方法正确处理它而不实际发出请求.

GetResponseAsync()返回一个Task<WebResponse>对象.所以我通常会这样做:

using (ShimsContext.Create())
{
    System.Net.Fakes.ShimWebRequest.AllInstances.GetResponseAsync = (x) =>
    {
        return new Task<WebResponse>(() =>
        {
            HttpWebResponse toRet = new HttpWebResponse();
            return toRet;
        });
    }
}
Run Code Online (Sandbox Code Playgroud)

问题是上面没有编译因为

'System.Net.HttpWebResponse.HttpWebResponse()'已过时:'此API支持.NET Framework基础结构,不能直接在您的代码中使用.

我知道这种类型已经过时,我们现在应该使用不同的东西,但我试图测试的遗留代码不允许我这么奢侈.我已经看过很多关于这个主题的问题,但似乎没有人回答这个问题.

c# unit-testing mocking httpwebrequest microsoft-fakes

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