大家.在最近学习OC时,我总是遇到这样的枚举.
enum {
type1 = 0,
type2 = 1 << 0,
type3 = 1 << 1,
};
Run Code Online (Sandbox Code Playgroud)
type = 1 << 0是什么意思?它通常用于什么?谢谢你.
我正在使用 Moq 来模拟 ASP.NET Core 项目中的对象。
我想模拟以下 IsConnection() 方法:
public Client(IMongoClient client)
{
_client = client;
}
public async Task<bool> IsConectionOk()
{
var pingCommand = new BsonDocument("ping", 1);
var mongoDb = _client.GetDatabase("Name");
var commandResult = await mongoDb.RunCommandAsync<BsonDocument>(pingCommand);
return commandResult != null;
}
Run Code Online (Sandbox Code Playgroud)
如您所见,只有一次注入,IMongoClient
所以我需要模拟这一注入。IMongoDatabase
现在,我还需要模拟,因为_client.GetDatabase
返回给我一个IMongoDatabase
运行的RunCommandAsync
这是我的单元测试:
[Fact]
public async Task IsConnectionOk_xxx_RunPing1Command()
{
var dbMock = new Mock<IMongoDatabase>();
var resultCommand = new BsonDocument("ok", 1);
dbMock.Setup(stub => stub.RunCommandAsync<BsonDocument>(It.IsAny<BsonDocument>(), It.IsAny<ReadPreference>(), It.IsAny<CancellationToken>())).ReturnsAsync(resultCommand);
var mongoClientMock = new Mock<IMongoClient>(); …
Run Code Online (Sandbox Code Playgroud) 几乎疯狂地搜索我的代码失败的地方......我能够隔离这种奇怪的行为.减去-200天减去的数量
NSDate *now = [NSDate date]; //now is 2013-07-19
NSDateFormatter *format = [[NSDateFormatter alloc] init];
[format setDateFormat:@"YYYY-MM-dd"];
NSDate *newDate1 = [now dateByAddingTimeInterval:60*60*24*-199];
newDateTmp=[format stringFromDate:newDate1];
NSLog(@"now:%@ newDateTmp:%@",now,newDateTmp);
newDate1 = [now dateByAddingTimeInterval:60*60*24*-200];
newDateTmp=[format stringFromDate:newDate1];
NSLog(@"now:%@ newDateTmp:%@",now,newDateTmp);
newDate1 = [now dateByAddingTimeInterval:60*60*24*-201];
newDateTmp=[format stringFromDate:newDate1];
NSLog(@"now:%@ newDateTmp:%@",now,newDateTmp);
newDate1 = [now dateByAddingTimeInterval:60*60*24*-365];
newDateTmp=[format stringFromDate:newDate1];
NSLog(@"now:%@ newDateTmp:%@",now,newDateTmp);
Run Code Online (Sandbox Code Playgroud)
日志:
2013-07-19 15:58:46.123 Vendes [2927:907]现在:2013-07-19 13:58:46 +0000 newDateTmp:2013-01-01 //这没关系
2013-07-19 15:58:46.124 Vendes [2927:907]现在:2013-07-19 13:58:46 +0000 newDateTmp:2013-12-31 //这是不正确的!看看年份
2013-07-19 15:58:46.125 Vendes [2927:907]现在:2013-07-19 13:58:46 +0000 newDateTmp:2012-12-30 //这没关系
2013-07-19 15:58:46.127 Vendes [2927:907]现在:2013-07-19 13:58:46 …
为什么通用代表有DelegateName<T>
?为什么不DelegateName(T arg)
参数已经指定了类型,那么为什么必须跟随委托名称<T>
,它是作为命名约定,还是开发人员知道它接受整数或这种C#语法的目的是什么?
public delegate void MyGenericDelegate<T>(T arg);
Main()
{
// Register targets.
MyGenericDelegate<string> strTarget = new MyGenericDelegate<string>(StringTarget);
strTarget("Some string data");
MyGenericDelegate<int> intTarget = new MyGenericDelegate<int>(IntTarget);
intTarget(9);
static void StringTarget(string arg)
{
Console.WriteLine("arg in uppercase is: {0}", arg.ToUpper());
}
static void IntTarget(int arg)
{
Console.WriteLine("++arg is: {0}", ++arg);
}
}
Run Code Online (Sandbox Code Playgroud) c# ×2
objective-c ×2
c ×1
date ×1
delegates ×1
foundation ×1
generics ×1
ios ×1
mongodb ×1
moq ×1
nsdate ×1
unit-testing ×1