moq

moq only one method in a class

大憨熊 提交于 2020-01-31 06:44:44
问题 I'm using moq.dll When I mock a class(all the IRepository interface) i use this line code int state = 5; var rep = new Mock<IRepository>(); rep.Setup(x => x.SaveState(state)).Returns(true); IRepository repository = rep.Object; but in this case i mock all the function in repository class. Then all the methods in class repository are substituted with the methods setup of Mock dll I want use all the methods defined in class repository(the real class) and mock only one function(SaveState) How can

Setting up a C# Test with Moq against Async methods

感情迁移 提交于 2020-01-30 08:17:08
问题 I'm trying to create a set of test methods using Moq to cover the external dependencies. These dependencies are async in nature and I have come across a set of them that when awaited they never return, so I'm not sure what I'm missing. The test itself is very simple. [TestMethod] public async Task UpdateItemAsync() { var repository = GetRepository(); var result = await repository.UpdateItemAsync("", new object()); Assert.IsNotNull(result); } The GetRepository method above is what sets up the

how do I mock sqlconnection or should I refactor the code?

痞子三分冷 提交于 2020-01-30 02:39:14
问题 I have the code below, I have read Moq and SqlConnection? and How can I stub IDBconnection, but I still have no idea how to mock the following sqlconnection. public class SqlBulkWriter : ISqlBulkWriter { private readonly string _dbConnectionString;; public SqlBulkWriter(string dbConnectionString) { this._dbConnectionString = dbConnectionString; } public void EmptyTable(string schema, string tableName) { using (var connection = new SqlConnection(this._dbConnectionString)) { try { connection

MOQ C# QUERIES It.IsAny returning a List

送分小仙女□ 提交于 2020-01-25 03:53:17
问题 I have made a lot of progress during the night. I finally managed to set up my tests. Now my code will execute the first test and end up in the CREATE METHOD of the controller. What I am trying to see if I need to change my controller method so it can take a full object (perhaps create a new IF statement checking if the admin KEY and Admin Name are not null. Do yo have any suggestions as to the design pattern and realistic Unit Testing design? Test: public void Creating_One_Note() { var note

Unit Testing Rich Domain Model

一个人想着一个人 提交于 2020-01-24 18:59:33
问题 This was the anemic domain model: public partial class Person { public virtual int PersonId { get; internal protected set; } public virtual string Title { get; internal protected set; } public virtual string FirstName { get; internal protected set; } public virtual string MiddleName { get; internal protected set; } public virtual string LastName { get; internal protected set; } } And this is its behavior: public static class Services { public static void UpdatePerson(Person p, string

Why can't I mock MouseButtonEventArgs.GetPosition() with Moq?

删除回忆录丶 提交于 2020-01-24 18:09:36
问题 I'm trying to mock MouseButtonEventArgs.GetPosition() with Moq, but I keep receiving this error: System.ArgumentException: Invalid setup on a non-overridable member: m => m.GetPosition(It.IsAny<IInputElement>()) at Moq.Mock.ThrowIfCantOverride(Expression setup, MethodInfo methodInfo) at Moq.Mock.<>c__DisplayClass12`2.<Setup>b__11() at Moq.PexProtector.Invoke<T>(Func`1 function) at Moq.Mock.Setup<T1,TResult>(Mock mock, Expression`1 expression) at Moq.Mock`1.Setup<TResult>(Expression`1

Why can't I mock MouseButtonEventArgs.GetPosition() with Moq?

假装没事ソ 提交于 2020-01-24 18:08:19
问题 I'm trying to mock MouseButtonEventArgs.GetPosition() with Moq, but I keep receiving this error: System.ArgumentException: Invalid setup on a non-overridable member: m => m.GetPosition(It.IsAny<IInputElement>()) at Moq.Mock.ThrowIfCantOverride(Expression setup, MethodInfo methodInfo) at Moq.Mock.<>c__DisplayClass12`2.<Setup>b__11() at Moq.PexProtector.Invoke<T>(Func`1 function) at Moq.Mock.Setup<T1,TResult>(Mock mock, Expression`1 expression) at Moq.Mock`1.Setup<TResult>(Expression`1

Why can't I mock MouseButtonEventArgs.GetPosition() with Moq?

我的未来我决定 提交于 2020-01-24 18:08:16
问题 I'm trying to mock MouseButtonEventArgs.GetPosition() with Moq, but I keep receiving this error: System.ArgumentException: Invalid setup on a non-overridable member: m => m.GetPosition(It.IsAny<IInputElement>()) at Moq.Mock.ThrowIfCantOverride(Expression setup, MethodInfo methodInfo) at Moq.Mock.<>c__DisplayClass12`2.<Setup>b__11() at Moq.PexProtector.Invoke<T>(Func`1 function) at Moq.Mock.Setup<T1,TResult>(Mock mock, Expression`1 expression) at Moq.Mock`1.Setup<TResult>(Expression`1

Even simple Moq code is throwing NotSupportedException

天大地大妈咪最大 提交于 2020-01-24 13:15:15
问题 I've been struggling to use Moq as a mocking framework and copied some very simple example code. I must be missing something really stupid here. It throws a NotSupportedException on the Setup call, even though it points to the Returns method. This code is part of my tests class: class Test { public string DoSomethingStringy(string s) { return s; } } [TestInitialize] public void Setup() { var mock = new Mock<Test>(); mock.Setup(x => x.DoSomethingStringy(It.IsAny<string>())) .Returns((string s)

How to mock IFindFluent interface

…衆ロ難τιáo~ 提交于 2020-01-24 11:28:46
问题 I am writing unit tests with mongo db and I need to test somehow methods that works with data returned from mongo. For example method IFindFluent<Transcript, Transcript> GetTranscriptByUserId(int userId); Should return some transcript. But instead it returns interface that has couple of props - Filter , Options , Sort , and other. I am going to test method Paginane of Paginator<T> class public PaginatedObject<T> Paginate(IFindFluent<T,T> items, int limit, int page) { if (limit == 0) { limit =