我正在尝试使用MOQ对以下ViewModel的LoginExecute方法进行单元测试
public class LoginViewModel : ViewModelBase, ILoginViewModel
{
INavigationService navigationService;
IDialogService dialogService;
IAdminService adminService;
public RelayCommand LoginCommand { get; set; }
private string _productID;
public string ProductID
{
get { return _productID; }
set
{
_productID = value;
RaisePropertyChanged("ProductID");
}
}
public LoginViewModel(INavigationService navigationService, IDialogService dialogService, IAdminService adminService)
{
this.navigationService = navigationService;
this.dialogService = dialogService;
this.adminService = adminService;
InitializeCommands();
}
private void InitializeCommands()
{
LoginCommand = new RelayCommand(() => LoginExecute());
}
public async Task LoginExecute()
{
await this.navigationService.TestMethod();
this.navigationService.Navigate(typeof(ITherapistsViewModel));
} …Run Code Online (Sandbox Code Playgroud) 我有一个实现的实例IDictionary<T, K>,我不知道编译时的T和K,并希望从中获取所有元素.我不想IEnumerable出于某种原因使用,这将是唯一实现的非通用接口IDictionary.
我到目前为止的代码:
// getting types
Type iDictType = instance.GetType().GetInterface("IDictionary`2");
Type keyType = iDictType.GetGenericArguments()[0];
Type valueType = iDictType.GetGenericArguments()[1];
// getting the keys
IEnumerable keys = (IEnumerable)dictType.GetProperty("Keys")
.GetValue(instance, null);
foreach (object key in keys)
{
// ==> this does not work: calling the [] operator
object value = dictType.GetProperty("Item")
.GetValue(instance, new object[] {key } );
// getting the value from another instance with TryGet
MethodInfo tryGetValue = iDictType.GetMethod("TryGetValue");
object[] arguments = new object[] { key, …Run Code Online (Sandbox Code Playgroud) 我有一个应用程序,用于SqlConnection.ClearAllPools在删除数据库之前关闭所有连接.
有一种情况仍然存在连接.此连接已在另一个应用程序域中创建.
所以我想知道哪些连接关闭了SqlConnection.ClearAllPools?
我有一些代码,它使用HttpWebRequest类的.BeginGetResponse()方法,它是异步的.此外,我正在使用Microsoft的Unit Test App项目来测试应用程序.
问题是测试框架没有等待运行异步代码的结束,所以我无法检查其结果.
我应该如何使用Unit Test App项目测试异步代码?我没有使用async/await modificators.
我的班级有一个类型的领域Dictionary<string, List<string>>.用NHibernate映射它的最佳方法是什么?我最好把它作为一个领域,不想暴露它.
非常感谢!
乌鲁
我有一个类,它包含所有现有实例的静态字典,这些实例是在编译时定义的.
基本上它看起来像这样:
[DataContract]
class Foo
{
private static Dictionary<long, Foo> instances = new Dictionary<long, Foo>();
[DataMember]
private long id;
public static readonly Foo A = Create(1);
public static readonly Foo B = Create(2);
public static readonly Foo C = Create(3);
private static Foo Create(long id)
{
Foo instance = new Foo();
instance.id = id;
instances.Add(instance);
return instance;
}
public static Foo Get(long id)
{
return instances[id];
}
}
Run Code Online (Sandbox Code Playgroud)
还有其他字段,派生类,但这对问题无关紧要.
只有id序列化.当反序列化此类型的实例时,我想获取已创建为静态字段(A,B或C)的Foo.Get(id)实例,而不是获取新实例. …
我准备将我的一些实体从身份转换为hilo id-generator.
我不知道如何设计保持下一个高值的表.
有没有最佳做法?需要考虑什么?任何方法都有任何优缺点吗?
一个小问题.任何想法的人为什么这不起作用?
int? nullableIntVal = (this.Policy == null) ? null : 1;
Run Code Online (Sandbox Code Playgroud)
null如果左手表达式为True,我试图返回,否则1.看似简单,但给出了编译错误.
无法确定条件表达式的类型,因为null和之间没有隐式转换int.
如果我用任何有效的替换nullin ,那么没有问题.? null : 1int
我有一些带有一些字段的结构.其中一个字段是泛型类型.泛型类型可以是引用类型或值类型.
我想强制它在内部存储为引用,以避免结构变得太大.
struct Foo<T>
{
T field; // should be a reference
}
Run Code Online (Sandbox Code Playgroud)
我知道我可以使用object或者T[],但两者都很尴尬.是不是像通用的参考类型?
struct Foo<T>
{
Reference<T> field;
}
Run Code Online (Sandbox Code Playgroud)
是的,当然,我可以写自己的.但我试图避免这种情况
我对项目中遇到的问题感到困惑.我试图简化它以重现效果:
interface IBar { }
class Bar : IBar {}
interface IFoo<T> where T : IBar { }
class Foo<T> : IFoo<T> where T : IBar { }
class Class1
{
public void DoTheFoo<T>(T bar) where T : IBar
{}
public void DoTheFoo<T>(IFoo<T> foo) where T : IBar
{}
public void Test()
{
var bar = new Bar();
var foo = new Foo<Bar>();
DoTheFoo(bar); // works
DoTheFoo<Bar>(foo); // works
DoTheFoo((IFoo<Bar>)foo); // works
DoTheFoo(foo); // complains
}
}
Run Code Online (Sandbox Code Playgroud)
对我来说这看起来很好,但编译器在最后一次调用时抱怨,因为它试图DoTheFoo<T>(T bar) …
c# ×7
generics ×2
nhibernate ×2
unit-testing ×2
.net ×1
ado.net ×1
asynchronous ×1
hilo ×1
idictionary ×1
mapping ×1
mocking ×1
mstest ×1
nullable ×1
overloading ×1
reference ×1
reflection ×1
sql-server ×1
value-type ×1