我有一些期望 EXPECT_CALL (...)
EXPECT_CALL(t1, foo()).Times(1);
Run Code Online (Sandbox Code Playgroud)
我想创造对面.我希望不会执行某个功能.
我应该使用什么方法?有点像EXPECT_NOT_CALL (...)
?
这是我的班级:
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) 我刚刚将依赖注入添加到我的应用程序中,并希望对我的方法运行模拟测试以确保它们正常工作.
我有一个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) 我有一个名为A的项目,它有一个名为ClassA的类. ClassA有一个名为ReadBlock()的方法,它创建一个CloudBlockBlob对象并调用其中一个方法.
CloudBlockBlob是一个位于Microsoft.WindowsAzure.Szrage.Blob命名空间的类,该命名空间位于Microsoft.WindowsAzure.Storage.dll中.
我的项目A有一个名为A.Tests的单元测试项目.现在,我想测试方法ReadBlock().为了测试它,我需要模拟CloudBlockBlob对象并拦截对其方法的调用,返回自定义值并验证方法是否被调用.
更新: 问题是我是否可以在不修改项目A的代码的情况下完成此操作.
谢谢!
我想为实现IDisposable的类编写单元测试.该类有许多私有字段,也实现IDisposable.在我的测试中,我想验证当我调用时Dispose()
,它正确调用Dispose()
其所有IDisposable字段.基本上,我希望我的单元测试看起来像这样:
var o = new ObjectUnderTest();
o.Dispose();
Assert.IsFalse(ObjectHasUndisposedDisposables(o));
Run Code Online (Sandbox Code Playgroud)
我正在考虑使用反射来实现这一目标.看起来这是一个相当普遍的要求,但我找不到它的任何例子.
有人试过吗?
编辑 - 我不想将Disposables注入被测试的类中.
我创建了一个函数,它返回剩下的秒数直到下一次出现,但是我遇到了为它编写单元测试的问题.人们如何测试这种有调用功能的功能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)
结果是. …
我是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) 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) 我在 (WebApi) 项目上使用NUnit
和RhinoMocks
进行单元测试。
有一种方法我正在尝试为其编写测试,它应该向 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
从测试方法中运行时如何使该方法可用。我怎样才能做到这一点?
另外,我如何检查项目是否已添加(我可以/应该使用什么样的断言)
我被指派为我无权修改的应用程序编写单元测试.我想要单元测试的方法进行如下调用:
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# ×8
unit-testing ×8
mocking ×6
assert ×4
moq ×2
nunit ×2
datetime ×1
dispose ×1
equality ×1
gmock ×1
googletest ×1
python ×1
reflection ×1
rhino-mocks ×1