Reputation: 341
I've been searching the web for a way to make an IEnumerable
in the Setup
of a Mock by converting the Values of my IDictionary
to it (either directly or conversion from '=List
to IEnumerable
). However I have only come across the latter.
Inspiration Sources:
How to use Moq to return a List of data or values?
public class UserServiceTests
{
private Mock<IUserRepository> _userRep { get; set; }
// fake app repository to get data from
private IDictionary<int, User> _userRepData { get; set; }
private UserService _sut { get; set; }
public UserServiceTests()
{
_userRepData = new Dictionary<int, User>();
_userRep = new Mock<IUserRepository>();
// Been able to create the proper list using:
// List<User> usersList = _userRepData.Values.ToList<User>();
// IEnumerable<User> users = appsList.AsEnumerable();
// So I am looking for away to replicate this in my setup method
// Obviously know the below is not the correct syntax.
_userRep.Setup(r => r.GetUsers()).Returns(() => new IEnumerable<User> {_userRepData.Values.ToList<User>} );
_sut = new UserService(_userRep.Object);
}
[Fact]
public void GetUsers_succeeds_at_getting_all_users_from_repository()
{
User user1 = new User();
User user2 = new User();
_userRepData.Add(1, user1);
_userRepData.Add(2, user2);
IEnumerable<User> users = new User[] { user1, user2 };
_sut.GetUsers().Should().BeSameAs(users); // Note: fluentassertions
}
}
Upvotes: 1
Views: 183
Reputation: 887957
Values
already implements IEnumerable<T>
.
You can return it directly; you don't need to create anything.
Upvotes: 1