[英]How can I mock properties which have no setters in unit testing?
I am trying to do unit testing for my universal app and am looking for a way to test a function that uses the Geocoordinate
class. 我正在尝试为通用应用程序进行单元测试,并且正在寻找一种测试使用
Geocoordinate
类的功能的Geocoordinate
。
Let's say you have the following function (which contains a lot more logic in my case, of course): 假设您具有以下功能(当然,就我而言,该功能包含更多逻辑):
public int GetAccuracy(Geocoordinate coord) {
return coord.Accuracy;
}
How can I unit test it? 如何进行单元测试? I can create a
Geocoordinate
object but I can't set an Accuracy
in my unit test method. 我可以创建一个
Geocoordinate
对象,但是不能在单元测试方法中设置Accuracy
。 Accuracy
and other properties like the latitude and longituge only have a getter. Accuracy
和其他特性(例如纬度和经度)只有吸气剂。
Is there an easy way to solve this? 有没有简单的方法来解决这个问题?
I would create my own abstract interface for Geocoordinate, and pass an instance of the interface to the GetAccuracy method. 我将为Geocoordinate创建自己的抽象接口,并将该接口的实例传递给GetAccuracy方法。 Your app would use an implementation that simply wraps a Geocoordinate instance, whilst your unit test would create a mocked version of the interface.
您的应用程序将使用仅包装Geocoordinate实例的实现,而单元测试将创建接口的模拟版本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.