我一直在网上搜索一种方法,通过将我的IEnumerable
的值转换为Setup
来直接转换为模拟的IDictionary
List
'= IEnumerable
到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
}
)。但是我只遇到了后者。
灵感来源:
答案 0 :(得分:1)
Values
已实施IEnumerable<T>
你可以直接退货;你不需要创造任何东西。