【发布时间】:2012-02-17 14:07:12
【问题描述】:
我在这里发疯了,我得到了这个例外。我正在使用 Typemock Isolator 和 Nunit 对 Sharepoint 2010 项目进行单元测试。我要模拟的是 UserProfileManager 的 UserProfile 集合。
要测试的代码:
public void IterateUsers()
{
SPServiceContext context = SPServiceContext.GetContext(site);
if (profileManager == null)
{
profileManager = new UserProfileManager(context);
}
foreach (UserProfile profile in profileManager)
{
DoThingsThatAreNotRelevant();
}
}
}
还有我的测试代码:
[Test]
public void IterateUsersTest()
{
//SPSite
var fakeSite = Isolate.Fake.Instance<SPSite>(Members.ReturnRecursiveFakes);
Isolate.Swap.NextInstance<SPSite>().With(fakeSite);
//SPServiceContext
var fakeSPServiceContext = Isolate.Fake.Instance<SPServiceContext> (Members.ReturnRecursiveFakes);
Isolate.WhenCalled(() => SPServiceContext.GetContext(fakeSite)).WillReturn(fakeSPServiceContext);
//UserProfileManager
var fakeUserProfileManager = Isolate.Fake.Instance<UserProfileManager>(Members.ReturnRecursiveFakes);
Isolate.Swap.NextInstance<UserProfileManager>().With(fakeUserProfileManager);
//UserProfile
var fakeUserProfile = Isolate.Fake.Instance<UserProfile>(Members.ReturnRecursiveFakes);
Isolate.Swap.NextInstance<UserProfile>().With(fakeUserProfile);
Isolate.WhenCalled(() => fakeUserProfileManager).WillReturnCollectionValuesOf(new[] {fakeUserProfile, fakeUserProfile, fakeUserProfile});
}
所以,我的计划是模拟 UserProfileManager 以返回一组 fakeUserProfile,这样我就可以遍历 foreach 循环。 fakeUserProfile 的内容并不重要,因为我可以毫无问题地模拟行为。
问题是,当它试图执行这一行时
Isolate.WhenCalled(() => fakeUserProfileManager).WillReturnCollectionValuesOf(new[] {fakeUserProfile, fakeUserProfile, fakeUserProfile});
我得到了一个很好的 ArgumentOutOfRangeException。我做错了什么?
【问题讨论】:
-
根据docs,您这样做的方式似乎是正确的。也许问题在于您创建它的方式。由于我从来没有使用过隔离器,我怕我帮不上忙。修复标签,它可能会引起人们的注意。
标签: visual-studio-2010 unit-testing sharepoint-2010 typemock