xUnit Test Add Model to List Failing - c#

I'm using xUnit to test my project. I have a test that checks if a user has been added the a list of users shown below:
private readonly IJsonService _jsonService;
private readonly IUserService _userService;
[Fact]
public void Add_User_To_User_List()
{
//Given
_userService = new UserService(_jsonService, _guidService);
_jsonService = Substitute.For<IJsonService>();
var _fakeUserJsonFile = "Users.json";
var _fakeNewUser = new User()
{
ID = new Guid(),
FirstName = "Denis",
LastName = "Menis"
};
var _fakeUserList = new List<User>
{
new User()
{
ID = new Guid(),
FirstName = "Paddy",
LastName = "Halle"
},
new User()
{
ID = new Guid(),
FirstName = "Job",
LastName = "Blogs"
}
};
var _fakeUpdatedUserList = new List<User>
{
new User()
{
ID = new Guid(),
FirstName = "Paddy",
LastName = "Halle"
},
new User()
{
ID = new Guid(),
FirstName = "Job",
LastName = "Blogs"
},
new User()
{
ID = new Guid(),
FirstName = "Denis",
LastName = "Menis"
}
};
_jsonService.DeserializeObject<User>(_fakeUserJsonFile).Returns(_fakeUserList);
_jsonService.SerializeObject(_fakeUserJsonFile, _fakeUpdatedUserList).Returns(true);
//When
var result = _userService.AddUser(_fakeNewUser);
//Then
Assert.Contains(_fakeNewUser, _fakeUpdatedUserList);
}
Now I know that the code works because I wrote it first but when I run my test it fails! Below is my code to add the user to the user list:
public bool AddUser(User user)
{
var userList = GetUsers();
user.ID = _guidService.NewGuid();
userList.Add(user);
var serializeObject = _jsonService.SerializeObject(_fileName, userList);
return serializeObject;
}
GetUser Method:
public List<User> GetUsers()
{
return _jsonService.DeserializeObject<User>(_fileName).ToList();
}
Deserialise Method:
private readonly IFileSystem _file;
private readonly HttpContextBase _httpContext;
private readonly ILogger _logger;
public JsonService(IFileSystem file, HttpContextBase httpContext, ILogger logger)
{
_file = file;
_httpContext = httpContext;
_logger = logger;
}
public IEnumerable<T> DeserializeObject<T>(string fileName)
{
try
{
var relativeFileName = _httpContext.Server.MapPath(fileName);
var readFile = _file.ReadAllText(relativeFileName);
var list = JsonConvert.DeserializeObject<List<T>>(readFile);
return list;
}
catch (Exception ex)
{
_logger.LogException(ex);
return null;
}
}
FileSystem class:
public class FileSystem : IFileSystem
{
public void WriteAllText(string path, string contents)
{
File.WriteAllText(path, contents);
}
public string ReadAllText(string path)
{
return File.ReadAllText(path);
}
}
When I run my test, var serializeObject = _jsonService.SerializeObject(_fileName, userList); from the AddUser method returns false every time.
I think it's doing this because even though it's the same data as the expected result, in memory it's a different reference to the same data.
Can anyone help me with this I want it to return the same referenced data. If I'm not being very clear I can elaborate more. Thanks

You're absolutely right that the problem is rooted in the fact that _fakeUserList and _fakeUpdatedUserList reference two completely different objects. You've configured _jsonService.SerializeObject to return true when it is passed a reference to _fakeUpdatedUserList - but you're actually passing a reference to (a modified) _fakeUserList.
Basically, _fakeUpdatedUserList is completely unnecessary. You can focus on _fakeUserList, since that is the object that gets provided to the SUT (via DeserializeObject<User>, presumably).
For example:
[Fact]
public void Add_User_To_User_List()
{
//Given
_userService = new UserService(_jsonService, _guidService);
_jsonService = Substitute.For<IJsonService>();
var _fakeUserJsonFile = "Users.json";
var _fakeNewUser = new User()
{
ID = new Guid(),
FirstName = "Denis",
LastName = "Menis"
};
var _fakeUserList = new List<User>
{
new User()
{
ID = new Guid(),
FirstName = "Paddy",
LastName = "Halle"
},
new User()
{
ID = new Guid(),
FirstName = "Job",
LastName = "Blogs"
}
};
_jsonService.DeserializeObject<User>(_fakeUserJsonFile).Returns(_fakeUserList);
_jsonService.SerializeObject(_fakeUserJsonFile, _fakeUserList).Returns(true); // Match the original _fakeUserList, since that is what gets passed in by the implementation
//When
var result = _userService.AddUser(_fakeNewUser);
//Then
Assert.Contains(_fakeNewUser, _fakeUserList); // Verify that the provided _fakeUserList has been modified
}
On a side note: you can actually remove a lot of the details from this test, as they're irrelevant to the functionality that is being tested. For example, _fakeUserList can initially be empty - it doesn't have to contain any dummy values. And you can use the default values for _fakeNewUser (i.e., without specifying FirstName etc.), as they're not referenced at all in this test.
Edit: Thanks for posting the additional code (for GetUsers, etc.). This code shows that you're invoking ToList() on the IEnumerable<User> that is returned by DeserializeObject<User>. This is why your mock object is not behaving as you expect: the list returned by ToList() is a completely separate list from _fakeUserList.
Additionally, I don't see anywhere in your test where _fakeUserJsonFile is being injected into the SUT. So the _fileName in AddUser might not be what you're expecting, as well.
To get around this, you either need to modify your design (e.g., to not call ToList), or modify the expected behavior in your test. While you may want to consider the possibility of the former, the latter might be easier.
For example:
// Match any filename (unless you have a way of getting _fakeUserJsonFile into the SUT)
// Match any list, as long as it contains the new user
_jsonService.SerializeObject(Arg.Any<string>(), Arg.Is<List<User>>(list => list.Contains(_fakeNewUser))).Returns(true);
//When
var result = _userService.AddUser(_fakeNewUser);
//Then
Assert.IsTrue(result); // Only returns true if the mock object is invoked as expected
// There is no way to verify the following assertion, because the test has no way of accessing the "updated" list
//Assert.Contains(_fakeNewUser, _fakeUserList);

Related

In gRPC unable to bind enum values from message to dontnet core Dto

I need to define a string array type dataType in Grpc message. not sure how to do. right now i am doing it as a
repeated string Title= 1,
here i need name field as string array Type. But it is showing error that it is, field is readonly type when bind data in it:
public override async Task<UserResponse> CreateUser(
UserModel request, ServerCallContext context)
{
var eventResponse = new UserResponse();
var createCmd = new CreateUserCommand
{
Model = new UserDto
{
Title = request.Title,
Id = request.Id,
}
}
}
here in title i need to bind data
The generated code from protoc here gives you something like:
private readonly RepeatedField<string> title_ = new RepeatedField<string>();
[DebuggerNonUserCodeAttribute]
public RepeatedField<string> Title {
get { return title_; }
}
So: Title is indeed read-only. This means that instead of assigning it, you should explore what APIs exist for adding to it - i.e.
var user = new UserDto
{
Id = request.Id,
}
user.Title.Add(request.Title);
// or AddRange, etc
You may still be able to use initializer syntax, too:
new UserDto
{
Id = request.Id,
Title = { request.Title }
}
(which is an .Add)

How to mock nested properties and objects and their functions?

I have the code below which I would like to test, but I'm not sure whether it is possible or not.
I have EF repositories and they are put together to a class as public properties. I don't know exactly whether it is bad solution or not, but it is easier to manage the code and its dependencies. Only the testability is still a question.
Purpose of my test is injecting data via
administrationRepository.ModuleScreen.GetAll()
method and catch the result. I know that it can be tested once it is deployed, but I want the tests in build time in order to have as fast feedback as possible.
I went through questions and answers here, but I cannot find answers. In my code I got to the point where the property is set up, but when I call the administrationRepoMock.Object.ModuleScreen.GetAll() ReSharper offers only the methods coming from Entitiy Framework and not the Moq related functions.
It is possible what I want? If so, how? Is my design suitable for this? If not can you give me articles, urls where I can see examples?
Repository:
public interface IModuleScreen
{
IEnumerable<DomainModel.Administration.ModuleScreen> GetAll();
}
public interface IAdministrationRepository
{
IModuleScreen ModuleScreen { get; }
}
public partial class AdministrationRepository : IAdministrationRepository
{
public virtual IModuleScreen ModuleScreen { get; private set; }
public AdministrationRepository( IModuleScreen moduleScreen )
{
this.ModuleScreen = moduleScreen;
}
}
Application:
public partial class DigitalLibraryApplication : IDigitalLibraryApplication
{
private IAdministrationRepository _administrationRepository;
private IMapper.IMapper.IMapper _mapper;
private IDiLibApplicationHelper _dilibApplicationHelper;
#region Ctor
public DigitalLibraryApplication( IAdministrationRepository administrationRepository, IMapper.IMapper.IMapper mapper, IDiLibApplicationHelper diLibApplicationHelper)
{
_administrationRepository = administrationRepository;
_mapper = mapper;
_dilibApplicationHelper = diLibApplicationHelper;
}
#endregion
public IEnumerable<ModuleScreenContract> GetModuleScreens()
{
//inject data here
IEnumerable<ModuleScreen> result = _administrationRepository.ModuleScreen.GetAll();
List<ModuleScreenContract> mappedResult = _mapper.MapModuleScreenToModuleScreenContracts(result);
return mappedResult;
}
}
Test code:
[Test]
public void ItCalls_ModuleRepository_Get_Method()
{
List<SayusiAndo.DiLib.DomainModel.Administration.ModuleScreen> queryResult = new List<SayusiAndo.DiLib.DomainModel.Administration.ModuleScreen>()
{
new DomainModel.Administration.ModuleScreen()
{
Id = 100,
},
};
var moduleScreenMock = new Mock<IModuleScreen>();
moduleScreenMock.Setup(c => c.GetAll()).Returns(queryResult);
administrationRepoMock.SetupProperty(c => c.ModuleScreen, moduleScreenMock.Object);
var mapperMock = new Mock<IMapper.IMapper.IMapper>();
var dilibApplicationHerlperMock = new Mock<IDiLibApplicationHelper>();
IDigitalLibraryApplication app = new DigitalLibraryApplication( administrationRepoMock.Object, mapperMock.Object, dilibApplicationHerlperMock.Object );
app.GetModules();
//issue is here
administrationRepoMock.Object.ModuleScreen.GetAll() //???
}
Here is a refactoring of your test that passes when run. You can update the pass criteria to suit you definition of a successful test.
[Test]
public void ItCalls_ModuleRepository_Get_Method() {
// Arrange
List<ModuleScreen> queryResult = new List<ModuleScreen>()
{
new ModuleScreen()
{
Id = 100,
},
};
//Building mapped result from query to compare results later
List<ModuleScreenContract> expectedMappedResult = queryResult
.Select(m => new ModuleScreenContract { Id = m.Id })
.ToList();
var moduleScreenMock = new Mock<IModuleScreen>();
moduleScreenMock
.Setup(c => c.GetAll())
.Returns(queryResult)
.Verifiable();
var administrationRepoMock = new Mock<IAdministrationRepository>();
administrationRepoMock
.Setup(c => c.ModuleScreen)
.Returns(moduleScreenMock.Object)
.Verifiable();
var mapperMock = new Mock<IMapper>();
mapperMock.Setup(c => c.MapModuleScreenToModuleScreenContracts(queryResult))
.Returns(expectedMappedResult)
.Verifiable();
//NOTE: Not seeing this guy doing anything. What's its purpose
var dilibApplicationHerlperMock = new Mock<IDiLibApplicationHelper>();
IDigitalLibraryApplication app = new DigitalLibraryApplication(administrationRepoMock.Object, mapperMock.Object, dilibApplicationHerlperMock.Object);
//Act (Call the method under test)
var actualMappedResult = app.GetModuleScreens();
//Assert
//Verify that configured methods were actually called. If not, test will fail.
moduleScreenMock.Verify();
mapperMock.Verify();
administrationRepoMock.Verify();
//there should actually be a result.
Assert.IsNotNull(actualMappedResult);
//with items
CollectionAssert.AllItemsAreNotNull(actualMappedResult.ToList());
//There lengths should be equal
Assert.AreEqual(queryResult.Count, actualMappedResult.Count());
//And there should be a mapped object with the same id (Assumption)
var expected = queryResult.First().Id;
var actual = actualMappedResult.First().Id;
Assert.AreEqual(expected, actual);
}

Mocking with moq, trying to pass an object to constructor having multiple parameter

I am trying to mock a method that returns a IEnumerable set of data, like a list of all codes.
There is an Interface ISystemService.cs that contains this method, a service class called SystemService.cs that has the method definition.
System under test is:
public static class CacheKeys
{
public const string ALLCURRENCYCODES = "CurrencyCodes";
}
public interface ICacheManager
{
T Get<T>(string key);
void Set(string key, object data, int cacheTime);
void Clear();
}
public interface ISessionManager
{
}
public interface IApplicationSettings
{
string LoggerName { get; }
int CacheTimeout { get; }
}
public class EFDbContext : DbContext
{
public DbSet<CurrencyCode> CurrencyCodes { get; set; }
}
public class CurrencyCode
{
public string Code { get; set; }
public string Description { get; set; }
public decimal CurrencyUnit { get; set; }
public int? DecimalPlace { get; set; }
public string BaseCurrencyCode { get; set; }
}
public interface ISystemService
{
IEnumerable<CurrencyCode> GetAllCurrencyCodes();
}
//SystemService.cs
public class SystemService : ISystemService
{
private readonly EFDbContext db;
private readonly ICacheManager cacheManager;
private readonly ISessionManager sessionManager;
private readonly IApplicationSettings appSettings;
public SystemService(EFDbContext dbContext, ICacheManager cacheManager, ISessionManager sessionManager, IApplicationSettings appSettings)
{
db = dbContext;
this.cacheManager = cacheManager;
this.sessionManager = sessionManager;
this.appSettings = appSettings;
}
public IEnumerable<CurrencyCode> GetAllCurrencyCodes()
{
var allCurrencyCodes = cacheManager.Get<IEnumerable<CurrencyCode>>(CacheKeys.ALLCURRENCYCODES);
if (allCurrencyCodes == null)
{
allCurrencyCodes = db.CurrencyCodes.ToList();
cacheManager.Set(CacheKeys.ALLCURRENCYCODES, allCurrencyCodes, appSettings.CacheTimeout);
}
return allCurrencyCodes;
}
Test Method
[TestMethod]
public void testCacheMiss()
{
List<CurrencyCode> currencycodes = new List<CurrencyCode>()
{
new CurrencyCode(){Id = 1, Code = "IND", Description = "India"},
new CurrencyCode(){Id = 2, Code = "USA", Description = "UnitedStates"},
new CurrencyCodes(){Id = 3, Code = "UAE", Description = "ArabEmirates"}
};
var mockEfContext = new Mock<EFDbContext>();
var mockCacheManager = new Mock<ICacheManager>();
var mockSessionManager = new Mock<ISessionManager>();
var mockAppSettings = new Mock<IApplicationSettings>();
// Setups for relevant methods of the above here, e.g. to test a cache miss
mockEfContext.SetupGet(x => x.CurrencyCodes)
.Returns(currencycodes); // Canned currencies
mockCacheManager.Setup(x => x.Get<IEnumerable<CurrencyCode>>(It.IsAny<string>()))
.Returns<IEnumerable<CurrencyCodes>>(null); // Cache miss
// Act
var service = new SystemService(mockEfContext.Object, mockCacheManager.Object,
mockSessionManager.Object, mockAppSettings.Object);
var codes = service.GetAllCodes();
// Assert + Verify
mockCacheManager.Verify(x => x.Get<IEnumerable<CurrencyCodes>>(
It.IsAny<string>()), Times.Once, "Must always check cache first");
mockEfContext.VerifyGet(x => x.CurrencyCodes,
Times.Once, "Because of the simulated cache miss, must go to the Db");
Assert.AreEqual(currencycodes.Count, codes.Count(), "Must return the codes as-is");
Since the defined constructor does not accept one parameter, how to pass the object as parameter? Please advice
If CodeService is under test, then you want to be mocking its dependencies, not the CodeService itself.
You'll need to provide Mocks for all of the dependencies of CodeService to the constructor, i.e.:
var currencycodes = new List<SomeCodes>
{
new CurrencyCodes(){Id = 1, Code = "IND", Description = "India"},
new CurrencyCodes(){Id = 2, Code = "USA", Description = "UnitedStates"},
new CurrencyCodes(){Id = 3, Code = "UAE", Description = "ArabEmirates"}
};
var mockEfContext = new Mock<EFDbContext>();
var mockCacheManager = new Mock<ICacheManager>();
var mockSessionManager = new Mock<ISessionManager>();
var mockAppSettings = new Mock<IApplicationSettings>();
// Setups for relevant methods of the above here, e.g. to test a cache miss
mockEfContext.SetupGet(x => x.SomeCodes)
.Returns(currencycodes); // Canned currencies
mockCacheManager.Setup(x => x.Get<IEnumerable<SomeCodes>>(It.IsAny<string>()))
.Returns<IEnumerable<SomeCodes>>(null); // Cache miss
// Act
var service = new CodeService(mockEfContext.Object, mockCacheManager.Object,
mockSessionManager.Object, mockAppSettings.Object);
var codes = service.GetAllCodes();
// Assert + Verify
mockCacheManager.Verify(x => x.Get<IEnumerable<SomeCodes>>(
It.IsAny<string>()), Times.Once, "Must always check cache first");
mockEfContext.VerifyGet(x => x.SomeCodes,
Times.Once, "Because of the simulated cache miss, must go to the Db");
Assert.AreEqual(currencycodes.Count, codes.Count(), "Must return the codes as-is");
Edit If you however mean that the next layer up of your code is under test, the principal is the same:
var mockCodeService = new Mock<ICodeService>();
mockCodeService.Setup(x => x.GetAllCodes())
.Returns(currencycodes); // Now we don't care whether this is from cache or db
var higherLevelClassUsingCodeService = new SomeClass(mockCodeService.Object);
higherLevelClassUsingCodeService.DoSomething();
mockCodeService.Verify(x => x.GetAllCodes(), Times.Once); // etc
Edit 2
I've fixed a couple of typos in the code, and assuming CurrencyCodes inherits SomeCodes and that your cache key is a string, and pushed it up onto a Git Gist here with the corresponding cache miss unit test as well. (I've used NUnit, but it isn't really relevant here)
allCodes is your service.. its the mock you need to be working with. You shouldn't be creating a concrete instance of your ICodeService.. your mock exists to fill that role.
So, remove this:
var service = new CodeService(allCodes.object);
Your next line should be:
var code = allCodes.Object.GetAllCodes();
But then.. this test seems completely redundant after that.. since you appear to be testing your mock..
Also, allCodes should be called serviceMock.. as that makes more sense.

Mocking a service call the object returns null

Hello :) I'm a novice in using Moq framework with Unit and I have an issue in which, as I will demonstrate below, I'm trying to Moq a service call on a MVC Controller which takes Session objects as parameters.
On my Unit test framework I create my object, set it up on the service call and I'm hoping to have it as the result of the response of the test to then Assert.
Problem: I tried to Mock HttpContext based on other solutions, which works because on the Controller side I get the values that I set on my Unit Test but upon SETUP of the service call (I have "Mock(MockBehavior.Strict);") when the debugger reaches the controller, upon the actual call I get an error saying that no SETUP was defined. Or if I take out the "MockBehavior.Strict", the "model" variable on the controller is always returning null and not the object I set it on the Unit Test class.
So here is my simple unit class,
[TestClass]
public class SearchControllerTest
{
#region Variables
Mock<ControllerContext> _controllerContext;
Mock<ISearchService> _serviceMock;
SearchController _controller;
#endregion
[TestInitialize]
public void SetUp()
{
// Arrange
_controllerContext = new Mock<ControllerContext>();
_serviceMock = new Mock<ISearchService>(MockBehavior.Strict);
_controller = new SearchController(_serviceMock.Object);
}
#region Success Test Cases
[TestMethod]
public void SearchListTest()
{
string pid = "val1";
string oid = "val2";
string lang = "val3";
string tid = "val4";
string pattern = "val5";
DocumentViewModel docModel = SetDocumentViewModel();
// Bypass
//_controllerContext.Setup(x => x.HttpContext.Session).Returns(_session.Object);
_controllerContext.SetupGet(p => p.HttpContext.Session["ProjectId"]).Returns("X");
_controllerContext.SetupGet(p => p.HttpContext.Session["OverlayId"]).Returns(string.Empty);
_controllerContext.SetupGet(p => p.HttpContext.Session["ProjectLanguage"]).Returns(string.Empty);
_controllerContext.SetupGet(p => p.HttpContext.Session["NodeId"]).Returns(string.Empty);
_controller.ControllerContext = _controllerContext.Object;
_serviceMock.Setup(x => x.FullTextSearchForAll(pid, oid, lang, tid, pattern)).Returns(docModel);
// Act
var result = _controller.SearchList(pid, oid, lang, tid, pattern) as PartialViewResult;
// Assert
Assert.AreEqual("#0Id", ((DocumentViewModel)result.Model).Rows[0].UID);
}
#endregion
#region Private
DocumentViewModel SetDocumentViewModel()
{
return new DocumentViewModel()
{
Columns = new Service.QueryResultColumn[]
{
new Service.QueryResultColumn
{
Alignment = ServiceConstants.Left,
Index = 0,
Visible = true,
Width = 3,
Header = ServiceConstants.Label
}
},
Properties = new DocumentsInfo[]
{
new DocumentsInfo()
{
IsCheckInAllowed = true,
IsCheckoutAllowed = true,
IsDocumentCheckedOut = false,
IsPlaceHolder = false,
IsUndoCheckoutAllowed = true,
lastVersionUid = "123"
}
},
Rows = new Service.QueryResultRow[]
{
new Service.QueryResultRow()
{
Children = null,
ExtensionData = null,
ImageSource = "Source",
Items = new Service.QueryResultItem[]
{
new Service.QueryResultItem()
{
ExtensionData = null,
ImageSource = "Src",
Text = "Txt",
UID = "uid"
}
},
UID = "#0Id"
}
}
};
}
#endregion
}
And here's my Controller,
public class SearchController : Controller
{
ISearchService _searchService;
public SearchController(ISearchService searchService) // I use UnityContainer
{
_searchService = searchService;
}
public PartialViewResult SearchList(string pid, string oid, string lang, string tid, string pattern)
{
ViewBag.ProjectId = pid;
ViewBag.OverlayId = oid;
ViewBag.ProjectLanguage = lang;
ViewBag.NodeId = tid;
ViewBag.Pattern = pattern;
DocumentViewModel model = null;
try
{
model = _searchService.FullTextSearchForAll(
Session["ProjectId"] as string,
Session["OverlayId"] as string,
Session["ProjectLanguage"] as string,
Session["ProjectId"] as string,
pattern
);
}
catch (Exception ex)
{
ViewBag.Error = ex.Message;
}
// Ajax.OnError() will handle the Custom Exception Error Message
if (ViewBag.Error != null)
throw new CustomtException((String)ViewBag.Error);
return PartialView(model);
}
}
Tank your for your patience and time.
Have a nice day :)
You've setup params in method with some values:
_serviceMock.Setup(x => x.FullTextSearchForAll(pid, oid, lang, tid, pattern)).Returns(docModel);
and trying to give Session variable as empty string
_controllerContext.SetupGet(p => p.HttpContext.Session["OverlayId"]).Returns(string.Empty);
it will never match. try setup service with It.IsAny() like
_serviceMock.Setup(x => x.FullTextSearchForAll(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>())).Returns(docModel);
And if it will shout change session setup
I recommend creating consts for ProjectId, et. al., and then using them to setup your mocks, verify calls, and set the state of any objects. This ensures the value you expect (and only the value you expect) is used throughout.

Having trouble getting method on mocked object to return fake data

I am trying to write a test for the "MethodToTest" like shown below
Public Class UserService()
{
private IRepository<User> _userRepo;
public UserService(IRepository<User> userRepo = null)
{
_userRepo = userRepo ?? new UserRepository();
}
public List<string> MethodToTest(string userName)
{
var user = _userRepo.Find(u => u.Email == userName).First<User>();
//Other stuff that eventually returns a List<string>
}
}
I have the following in my test
[Test]
public void GetItemsByUserName_UserName_ListOfItems()
{
var userName = "AnyString";
var fakeUserRepo = new Mock<IRepository<User>>();
var fakeUserList = new List<User>()
{
new User()
{
Email = userName,
Roles = new List<Role>()
{
new Role()
{
Name="Role1"
},
new Role()
{
Name="Role2"
}
}
}
};
var fakeUserListQueryable = fakeUserList.AsQueryable<User>();
var query = new Func<User, bool>(u => u.Email == userName);
fakeUserRepo.Setup(u => u.Find(query)).Returns(fakeUserListQueryable);
var userService = new UserService(fakeUserRepo.Object);
var menu = userService.GetMenuByUserName(userName);
//Assert Something
}
The problem is that I can't get the find method of the _userRepo to return my fake list of users.
When running the test I get a "Sequence Contains No Elements" when executing
_userRepo.Find(u => u.Email == userName).First<User>();
in the MethodToTest.
What am I doing wrong?
I'm thinking that you have an equality issue on the Func<User, bool>. As you know, Moq will setup the fakeUserRepo if the query equals the actual Func inside the code you are calling. However, you are creating a new Func<User, bool> in your setup code. So, when Moq checks equality, it is checking between two different reference types and therefore not setting up the expectation.
I would try something like this:
fakeUserRepo.Setup(u => u.Find(It.IsAny<Func<User, bool>>())).Returns(fakeUserListQueryable);
See if that works and then add the username back in.

Categories