使用DI进行单元测试(Using DI for Unit Testing)
我理解使用DI作为依赖项并将它们模拟为单元测试。 但是当我有多个当前功能的实现时,我正在进行单元测试,如何将它们注入到单元测试中。
例如:QuickSort或MergeSort
public class TestSort { ISort sort = null; [TestInitialize] public void Setup() { sort = new MergeSort(); // or any implementation which need to be injected on setup }
I understand using DI for dependencies and mocking them for unit tests. However when I have multiple implementations for the current feature which I am unit testing how do I inject them into the unit test.
Ex: QuickSort or MergeSort
public class TestSort { ISort sort = null; [TestInitialize] public void Setup() { sort = new MergeSort(); // or any implementation which need to be injected on setup }
原文:https://stackoverflow.com/questions/39349589
最满意答案
使用ObservableCollection的IndexOf方法:
int index = Bowls.IndexOf((Bowl)param);
I figured it out! Here's what caused the problem:
SowCommand = new DelegateCommand(param => SowGame(param));
To be able to get the index of the pressed button (which is bound to the above command) you need two things:
First you need to implement a function which somehow computes the index of an element in a collection. For example in my case:
public Int32 Number { get { if (Y == 0) { return _Size - 1 - X; } else { return _Size + X; } } }
Of course the method of getting the index depends on the problem. The above code is a part of the implementation of my Bowl type.
After this you have to add
Command Parameter="{Binding Number}"
to your .xaml and the code in the question should work properly.
相关问答
更多-
它必须只是一个参考。 否则,messenger必须知道如何克隆每个对象。 但你可以轻松检查它。 在详细信息视图模型中获取对象后,请进行更改。 添加内容,删除内容并更改集合中某些对象的某些参数。 然后使用网格检查主窗体中是否已更改。 It has to be just a reference. Otherwise messenger would have to know how to clone every single object. But you can easilly check it. After ...
-
使用IValueConverter过滤ObservableCollection(Filtering an ObservableCollection by using an IValueConverter)[2023-09-05]
如果绑定本身不支持绑定,您可以更改Appointments属性的类型,以便它包含您想要的参数。 如果您的转换器只被调用一次 - 您可以尝试订阅ObservableProperty的CollectionChanged事件,并在每次发生时为Appointments引发PropertyChanged事件。 If binding itself does not support bindings you could change the type of the Appointments property so it ... -
如何获取observablecollection中的对象索引作为参数传递(How to get index of object in observablecollection passed as parameter)[2023-12-16]
使用ObservableCollection的IndexOf方法: int index = Bowls.IndexOf((Bowl)param); I figured it out! Here's what caused the problem: SowCommand = new DelegateCommand(param => SowGame(param)); To be able to get the index of the pressed button (which is bound to th ... -
如何从ObservableCollection中获取第一个对象?(How to get the first object from an ObservableCollection?)[2022-05-31]
First不是属性它是扩展方法。 var first = CustomerDataObject.First(); 如果没有元素, First将抛出异常,在这种情况下你可以使用FirstOrDefault返回null。 var firstOrDefault = CustomerDataObject.FirstOrDefault(); First is not a property it is an extension method. var first = CustomerDataObject.First ... -
重构第一个集合,如下所示: class NamedObservableCollection : ObservableCollection
> { public string Name { get; private set;} public NamedObservableCollection(string name) { Name = name; } } 如果您在Convert方法的第一行放置了断点,则会发现Parameters始终为null 。 那是因为你永远不会初始化它。 解决方法是在构造函数中初始化该属性(或者在声明中,如果您使用C#6)。 Parameters不需要是ObservableCollection,因为没有人会永远绑定它,并且它也不需要是非泛型的子类。 最后, ProvideValue()应该提供具有您从XAML给出的Parameters的实际实例。 如果您返回一个空的Parameters集合的新实例,它将不会有任何Paramete ... 它应该是: auto res = { (func.template operator()::type&, index>(get (tuple)), nullptr)... }; 现场演示 。 更简单 /* Case #4: "Each with index as compile-time parameter" callback */ template 您正在克隆该集合,但您没有克隆其项目。 要创建集合的深层克隆,您还需要克隆项目(如果可能): foreach (var item in (IEnumerable)obj) { ICloneable cloneable = item as ICloneable; if (cloneable != null) { newC.Add(cloneable.Clone()); } else { newC.Add(item); } } 使用ICl ...绑定到ObservableCollection的ComboBox不会更新(ComboBox bound to a ObservableCollection does not update)[2022-04-30]
您正在尝试跟踪采样频率指数。 然后你必须绑定到具有这样的属性的对象并实现INotifyPropertyChanged.Or,正如我已经说过的,将此事件传播到作为绑定源的对象,并在其上引发正确的propertychanged。 否则,绑定引擎将不知道该属性的更改。 使用Path = SamplingFrequencyIndex绑定到CollectionControl.Settings You are trying to track samplingfrequency index. then you must ...如果启用CLR Exceptons在抛出时中断(在Debug | Exceptions下),您将看到此堆栈跟踪: mscorlib.dll!System.ThrowHelper.ThrowArgumentOutOfRangeException(System.ExceptionArgument argument, System.ExceptionResource resource) + 0x10 bytes mscorlib.dll!System.ThrowHelper.ThrowArgumentOutOf ...相关文章
更多- 在Hadoop中使用MRUnit进行单元测试
- 学习di'z地址
- 【第三章】 DI 之 3.1 DI的配置使用 ——跟我学spring3
- 【第三章】 DI 之 3.3 更多DI的知识 ——跟我学spring3
- rails 单元测试
- 《领测软件测试网软件测试公开课》(Course of Software Testing)[MP4]
- Gradle单元测试
- 【第三章】 DI 之 3.2 循环依赖 ——跟我学spring3
- 【第三章】 DI 之 3.4 Bean的作用域 ——跟我学spring3
- TestSuite使用教程-junit单元测试
最新问答
更多- 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
- 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
- OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
- 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
- codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
- 在计算机拍照在哪里进入
- 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
- No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
- 单页应用程序:页面重新加载(Single Page Application: page reload)
- 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
- System.StackOverflow错误(System.StackOverflow error)
- KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
- 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
- android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
- TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
- 企业安全培训的各项内容
- 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
- C#类名中允许哪些字符?(What characters are allowed in C# class name?)
- NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
- 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
- 将多个行和可变行移动到列(moving multiple and variable rows to columns)
- 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
- 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
- Angular $资源不会改变方法(Angular $resource doesn't change method)
- 在Angular 5中不是一个函数(is not a function in Angular 5)
- 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
- 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
- 常见的python rpc和cli接口(Common python rpc and cli interface)
- Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
- 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)