I spent an evening trying to mock an object that implements IQueryable: public interface IRepo<T> : IQueryable<T> { } The best I could come up with is something like this: var items = new Item[] {}.AsQueryable(); var repo = new Mock<IRepo>(); repo.Setup(r => r.GetEnumerator()).Returns(items.GetEnumerator()); repo.Setup(r => r.Provider).Returns(items.Provider); repo.Set
我花了一个晚上试图模拟一个实现IQueryable的对象: public interface IRepo<T> : IQueryable<T> { } 我能想到的最好的是这样的: var items = new Item[] {}.AsQueryable(); var repo = new Mock<IRepo>(); repo.Setup(r => r.GetEnumerator()).Returns(items.GetEnumerator()); repo.Setup(r => r.Provider).Returns(items.Provider); repo.Setup(r => r.ElementType).Returns(items.ElementTyp
This is not really specific to Moq but more of a general Mocking framework question. I have created a mock object for an object of type, "IAsset". I would like to mock the type that is returned from IAsset 's getter, "Info". var mock = new Mock<IAsset>(); mock.SetupGet(i => i.Info).Returns(//want to pass back a mocked abstract); mock.SetupPrope
这不是Moq特有的,而是更多的一般性Mocking框架问题。 我为类型为“IAsset”的对象创建了一个模拟对象。 我想嘲笑从IAsset的getter,“Info”返回的类型。 var mock = new Mock<IAsset>(); mock.SetupGet(i => i.Info).Returns(//want to pass back a mocked abstract); mock.SetupProperty(g => g.Id, Guid.NewGuid()); 我遇到的问题是模仿这个返回的属性值。 mock.SetupGet(i => i.Info).Ret
I'm facing difficulties in Silverlight (inbrowser) UnitTesting using Mock to read a file into my ViewModel. It gives me an AccessDenied error message. Is there another alternative method for that kind of problem? My UnitTesting is DragAndDrop Image file in Silverlight 4. eg: unittesing.cs var fileInfo = new Mock(); //I Can't Mock FileInfo var fileInfo = new FileInfo("test.j
我在使用Mock在我的ViewModel中读取文件的Silverlight(inbrowser)UnitTesting中遇到困难。 It gives me an AccessDenied error message. Is there another alternative method for that kind of problem? My UnitTesting is DragAndDrop Image file in Silverlight 4. 例如:unittesing.cs var fileInfo = new Mock(); //我无法模拟FileInfo var fileInfo = new FileInfo(“test.jpg”); 谢谢Jonny,我按照以下
Consider this scenario. I have an object, lets call it.... Foo. Foo raises a simple event named "Loaded". As part of the information for the event, consumers will need to know which foo object raised the event. Our team has adopted the following pattern. 1) Create a new class that inherits from EventArgs - for example, FooEventArgs : System.EventArgs. 2) Add a property of type F
考虑这种情况。 我有一个对象,让我们叫它...... Foo。 Foo提出了一个名为“Loaded”的简单事件。 作为事件信息的一部分,消费者需要知道哪个foo对象引发了事件。 我们的团队采用了以下模式。 1)创建一个从EventArgs继承的新类 - 例如,FooEventArgs:System.EventArgs。 2)将Foo类型的属性添加到FooEventArgs中,通过构造函数传入该属性。 3)使用通用版本的EventHandler声明事件,所以 public event EventHandler<
I have some code that raises PropertyChanged events and I would like to be able to unit test that the events are being raised correctly. The code that is raising the events is like public class MyClass : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; protected void NotifyPropertyChanged(String info) { if (PropertyChanged != null)
我有一些提出PropertyChanged事件的代码,我希望能够单元测试事件正在被正确提出。 引发事件的代码就像 public class MyClass : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; protected void NotifyPropertyChanged(String info) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(info)); }
How does a virtual event work? How would you override it? How would that work? And in what cases would you do that? Would it for example be an ok replacement for protected OnEvent methods? So that inheriting classes could just override the event and raise it directly? Or would that be wrong or just not work? The MSDN says this about it: An event can be marked as a virtual event by usin
虚拟事件如何工作? 你会如何覆盖它? 这将如何工作? 你会在什么情况下这样做? 它会例如是一个可以替代受保护的OnEvent方法吗? 所以继承类可以直接覆盖事件并直接引发它? 或者那会是错的或者只是不工作? MSDN对此说明了这一点: 通过使用virtual关键字,可以将事件标记为虚拟事件。 这使派生类可以使用override关键字覆盖事件行为。 覆盖虚拟事件的事件也可以被密封,这指定了派生类不再是虚拟的。 但是这并
Assuming the following: A class has managed only members. Some members implement IDisposable . The class is sealed - a class can't derive from and add unmanaged resources. The object is used inside a using statement - ie Dispose() is called when done. There are 3 possible implementations of IDisposable for this class: Minimal Dispose method that calls Dispose() on IDisposable memb
假设如下: 一个班级只管理成员。 一些成员实施IDisposable 。 该类是sealed - 一个类不能派生并添加非托管资源。 该对象在using语句中using - 即完成时调用Dispose() 。 这个类有3种可能的IDisposable实现: 在IDisposable成员上调用Dispose()最小Dispose方法 - 无终结器 。 Finalizer的标准IDisposable实现但缺少 Dispose()通常的GC.SuppressFinalize(this)调用。 Finalizer的完整标准IDisposable实现(以及Di
This is about ReSharper's warning "Access to disposed closure" which usually appears when an object which is later disposed is used in a lambda. Access to disposed closure in C#? discusses this in a bit more detail. My question is: For methods that take such lamdbas and execute them immediately (so you can be sure they are always executed before the said object is disposed): I
这是关于ReSharper的警告“访问配置封闭”,这通常是在一个lambda中使用后来处理的对象时出现的。 在C#中使用处置闭包? 更详细地讨论这一点。 我的问题是:对于采用这种lamdbas并立即执行它们的方法(这样可以确保它们始终在处理该对象之前执行): 有没有办法将它们标记为安全的,以便任何使用该方法的代码不再产生这些警告? 例: using (var myObject = new MyDisposableObject()) { DoThisTwice(() => myObje
System.Net.Http.HttpClient and System.Net.Http.HttpClientHandler in .NET Framework 4.5 implement IDisposable (via System.Net.Http.HttpMessageInvoker). The using statement documentation says: As a rule, when you use an IDisposable object, you should declare and instantiate it in a using statement. This answer uses this pattern: var baseAddress = new Uri("http://example.com"); var cookieCont
System.Net.Http.HttpClient和.NET Framework 4.5中的System.Net.Http.HttpClientHandler实现IDisposable(通过System.Net.Http.HttpMessageInvoker)。 using说明文档说: 通常,当您使用IDisposable对象时,您应该在using语句中声明并实例化它。 这个答案使用这种模式: var baseAddress = new Uri("http://example.com"); var cookieContainer = new CookieContainer(); using (var handler = new HttpClientHandler()
Introduction I just thought of a new design pattern. I'm wondering if it exists, and if not, why not (or why I shouldn't use it). I'm creating a game using an OpenGL. In OpenGL, you often want to "bind" things -- ie, make them the current context for a little while, and then unbind them. For example, you might call glBegin(GL_TRIANGLES) then you draw some triangles, t
介绍 我只是想到了一种新的设计模式。 我想知道它是否存在,如果不存在,为什么不(为什么我不应该使用它)。 我正在使用OpenGL创建游戏。 在OpenGL中,你经常想要“绑定”一些东西 - 例如,使它们成为当前的上下文一段时间,然后解除它们。 例如,你可以调用glBegin(GL_TRIANGLES)然后绘制一些三角形,然后调用glEnd() 。 我喜欢缩进其中的所有内容,以便清楚它开始和结束的位置,但是随后我的IDE喜欢取消它们,因为没有