考虑以下课程:
public class MyIntSet
{
private List<int> _list = new List<int>();
public void Add(int num)
{
if (!_list.Contains(num))
_list.Add(num);
}
public bool Contains(int num)
{
return _list.Contains(num);
}
}
Run Code Online (Sandbox Code Playgroud)
遵循"仅测试一件事"原则,假设我想测试"添加"功能.考虑以下这种测试的可能性:
[TestClass]
public class MyIntSetTests
{
[TestMethod]
public void Add_AddOneNumber_SetContainsAddedNumber()
{
MyIntSet set = new MyIntSet();
int num = 0;
set.Add(num);
Assert.IsTrue(set.Contains(num));
}
}
Run Code Online (Sandbox Code Playgroud)
我对这个解决方案的问题是它实际上测试了两个方法:Add()和Contains().从理论上讲,两者都可能存在错误,只会出现在一个接一个不被调用的情况下.当然,Contains()现在服务器作为List的Contains()的瘦包装器,它本身不应该进行测试,但如果它将来会变得更复杂呢?也许应该始终保留一个简单的"薄包裹"方法用于测试目的?
另一种方法可能建议模拟或暴露(可能使用InternalsVisibleTo或PrivateObject)私有_list成员并让测试直接检查它,但如果有一天内部列表被其他一些集合替换(可能是C5),那么可能会产生测试可维护性问题).
有一个更好的方法吗?我反对上述实现的任何论据都有缺陷吗?
在此先感谢,JC
注意:为简洁起见,以下内容无法区分随机性和伪随机性.此外,在此上下文中,约束意味着给定的最小值和最大值之间)
本System.Random类提供随机生成整数,双打和字节数组.使用Random.Next,可以轻松生成类型为布尔值,字符,(S)字节,(U)Int16,(U)Int32的随机约束值.使用时Random.NextDouble(),可以类似地生成Double和Single类型的约束值(就我对此类型的理解而言).随机串生成(给定长度和字母的)已经 也 被 解决 之前.
考虑剩余的原始数据类型(不包括Object):Decimal和(U)Int64.他们的随机生成也已被解决(Decimal,(U)Int64使用Random.NextBytes()),但不受限制.理论上可以使用拒绝采样(即循环直到生成的值是所需范围),但这显然不是一个实际的解决方案.归一化NextDouble()不起作用,因为它没有足够的有效数字.
简而言之,我要求正确实现以下功能:
long NextLong(long min, long max)
long NextDecimal(decimal min, decimal max)
Run Code Online (Sandbox Code Playgroud)
请注意,由于System.DateTime基于ulong,第一个函数也允许随机约束生成此类结构(类似于此处,仅在刻度而不是分钟内).
通过锁定助手,我指的是可以通过using语句实现锁定的一次性对象.例如,考虑Jon Skeet的MiscUtilSyncLock类的典型用法:
public class Example
{
private readonly SyncLock _padlock;
public Example()
{
_padlock = new SyncLock();
}
public void ConcurrentMethod()
{
using (_padlock.Lock())
{
// Now own the padlock - do concurrent stuff
}
}
}
Run Code Online (Sandbox Code Playgroud)
现在,考虑以下用法:
var example = new Example();
new Thread(example.ConcurrentMethod).Start();
Run Code Online (Sandbox Code Playgroud)
我的问题是这个 - 因为example是在一个线程上创建并ConcurrentMethod在另一个线程上调用,所以ConcurrentMethod线程不会忘记_padock在构造函数中的赋值(由于线程缓存/读写重新排序),因而抛出一个NullReferenceException(在_padLock本身)?
我知道锁定Monitor/ lock具有内存障碍的好处,但是当使用这样的锁定助手时,我无法理解为什么会有这样的障碍得到保证.在这种情况下,据我所知,构造函数必须修改:
public Example()
{
_padlock = new SyncLock();
Thread.MemoryBarrier();
}
Run Code Online (Sandbox Code Playgroud)
来源: …
考虑
Action _captureAction;
private void TestSimpleCapturedAction()
{
Action action = new Action(delegate { });
Action printAction = () => Console.WriteLine("Printing...");
action += printAction;
CaptureActionFromParam(action);
action -= printAction;
_captureAction(); //printAction will be called!
}
private void CaptureActionFromParam(Action action)
{
_captureAction = () => action();
}
Run Code Online (Sandbox Code Playgroud)
printAction将由_captureAction调用的原因是该行
action -= printAction;
Run Code Online (Sandbox Code Playgroud)
实际上翻译成
action = (Action) Delegate.Remove(action, printAction);
Run Code Online (Sandbox Code Playgroud)
因此,CaptureActionFromParam()中_captureAction捕获的操作不会更改 - 只会影响TestSimpleCapturedAction()中的本地"action"变量.
在这种情况下我想要的行为是printAction没有被调用.我能想到的唯一解决方案是定义一个新的"委托容器"类:
class ActionContainer
{
public Action Action = new Action(delegate { });
}
private void TestCapturedActionContainer()
{
var actionContainer = new ActionContainer();
Action …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用WMI和C#从远程计算机的事件查看器获取通知。我能够连接系统,也可以使用来获取事件日志ManagementObjectSearcher。但是,当我尝试使用ManagementEventWatcher.Start方法时,出现了一个异常:
访问被拒绝。(来自HRESULT的异常:0x80070005(E_ACCESSDENIED))
我已经在WMI控制中root\cimv2赋予了权限,并且还赋予了DCOM Config中用户帐户的管理员权限。
我有普通的Windows应用程序,因此我不使用ASP.net(ASPNET用户)。
我的代码是:
ConnectionOptions connectionOptions = new ConnectionOptions();
connectionOptions.Username = @"Domain\UName";//txtUserName.Text;
connectionOptions.Password = "pass";//txtPassword.Text;
connectionOptions.Impersonation = ImpersonationLevel.Impersonate;
ManagementScope managementScope = new ManagementScope(@"\\server\root\cimv2",connectionOptions);
managementScope.Options.EnablePrivileges = true;
managementScope.Connect(); // this line is executing fine.
eventWatcher = new ManagementEventWatcher(managementScope, new EventQuery("Select * From __InstanceCreationEvent WHERE TargetInstance ISA 'Win32_NTLogEvent' and TargetInstance.LogFile = 'Application'"));
eventWatcher.EventArrived += new EventArrivedEventHandler(Arrived);
eventWatcher.Scope.Options.EnablePrivileges = true;
eventWatcher.Start(); // Error occurs here
Run Code Online (Sandbox Code Playgroud) 当在"foreach"循环中删除时,在删除Azure表中的实体时,我没有得到一致的结果.PartitionKey是唯一的.
是否有人可以推荐的最佳做法?
为简洁起见,我遗漏了Try..Catch语句.
假设:
var context = new FooContext(_storageAccount);
var query = (from e in context.TableBar
where e.RowKey == rowKey
select e).AsTableServiceQuery();
Run Code Online (Sandbox Code Playgroud)
方法#1
foreach (var entity in query.Execute())
{
// delete each entity
context.DeleteObject(entity);
context.SaveChanges();
}
Run Code Online (Sandbox Code Playgroud)
方法#2
foreach (var entity in query.Execute())
{
// delete each entity
context.DeleteObject(entity);
}
context.SaveChanges();
Run Code Online (Sandbox Code Playgroud)
方法#3
var bars = query.Execute();
foreach (var bar in bars)
context.DeleteObject(bar);
context.SaveChanges();
Run Code Online (Sandbox Code Playgroud)
方法#1似乎删除了大多数实体,但通常最后一个实体不会删除.这是一个有效的实体.
我有一个 ASP.NET Core 1.1 应用程序。
它的 .csproj 有一个条目
<EnableDefaultContentItems>false</EnableDefaultContentItems>
Run Code Online (Sandbox Code Playgroud)
当我在网上搜索这个时,我发现的只是关于重复内容错误的问题。什么是被启用的默认项(或者更确切地说,未启用)在这里?而且,微软是否在我应该知道的地方记录了这个?
假设您获得了一个由以下简单代码编译的Class.dll程序集:
namespace ClassLibrary
{
public class Class
{
}
}
Run Code Online (Sandbox Code Playgroud)
并考虑使用上述Class.dll作为项目引用的不同项目,并使用以下代码:
Assembly assembly = Assembly.LoadFrom(@"Class.dll");
Type reflectedType = assembly.GetType("ClassLibrary.Class");
Type knownType = typeof(ClassLibrary.Class);
Debug.Assert(reflectedType == knownType);
Run Code Online (Sandbox Code Playgroud)
断言失败了,我不明白为什么.
如果我用System.Text.RegularExpressions.Regex类和带有System.dll的Class.dll替换ClassLibrary.Class,断言会成功,所以我猜它与项目属性有关吗?一些编译开关也许?
提前致谢
我使用以下脚本在OSX Yosemite 10.10.5上运行XCode(7.0.1 7A1001):
export FOO=bar #this should not be necessary, but just in case
launchctl setenv FOO bar #should make it visible to all GUI applications (=XCode)
open -a xcode
Run Code Online (Sandbox Code Playgroud)
然后我打开一个包含两个项目的工作区:App1和App2.在这两个项目中,我都放入$(HOME)/$(FOO)了Header Search Paths字段.
/Users/ohads/bar按预期解决./Users/ohads/- 注意HOME变量是如何解析的,但FOO变量不是.这里发生了什么?为什么会出现差异?我如何FOO在App2中工作 - 是否有一些我失踪的特殊标志或声明?
顺便说一句,好像这并不奇怪,App1即使在我只使用时也能正常工作export(相launchctl对于GUI应用程序应该使用的那个,看起来export应该只影响现金应用程序).
我使用下面的Azure Powershell cmdlet来验证ARM模板json和ARM模板params json文件.
$result = Test-AzureRmResourceGroupDeployment -ResourceGroupName TestRG -TemplateFile TestARMTemplate.json -ApiVersion TestARMParams.json
Run Code Online (Sandbox Code Playgroud)
如果两个输入参数都有效,我希望cmdlet返回true(布尔类型).
但是,结果是空的.
该文档还不清楚此cmdlet的预期响应.
我想知道我得到的回应是否是预期的回应.
注意:我在Windows 10计算机上使用Azure PowerShell 1.5版(2016年6月).
powershell azure azure-powershell azure-resource-manager azure-rm-template
c# ×6
asp.net-core ×1
azure ×1
bash ×1
delegates ×1
locking ×1
msbuild ×1
powershell ×1
random ×1
reflection ×1
types ×1
unit-testing ×1
wmi ×1
xcconfig ×1
xcode ×1