xUnit将IDictionary转换为IEnumerable

时间:2015-07-10 21:39:13

标签: c# moq xunit.net

我一直在网上搜索一种方法,通过将我的IEnumerable的值转换为Setup来直接转换为模拟的IDictionary List '= IEnumerablepublic 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 } )。但是我只遇到了后者。

灵感来源:

  1. IEnumerable<object> a = new IEnumerable<object>(); Can i do this?
  2. Convert dictionary with List to IEnumerable
  3. Convert dictionary of any type to IEnumerable collection
  4. How to use Moq to return a List of data or values?

    .git

    }

1 个答案:

答案 0 :(得分:1)

Values已实施IEnumerable<T> 你可以直接退货;你不需要创造任何东西。