【问题标题】:Unit Testing Sharepoint object UserProfileManager单元测试 Sharepoint 对象 UserProfileManager
【发布时间】: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


【解决方案1】:

行:

 Isolate.WhenCalled(() => fakeUserProfileManager).WillReturnCollectionValuesOf(new[] {fakeUserProfile, fakeUserProfile, fakeUserProfile});

错了!您需要指定一个方法才能使其工作,例如:

Isolate.WhenCalled(() => fakeUserProfileManager.SomeMethod()).WillReturnCollectionValuesOf

您不能告诉它只从该实例上的特定方法返回一个假实例的值。

【讨论】:

  • 我也这么认为,即使该行与文档中的行完全一样,所以我尝试了这一行:> Isolate.WhenCalled(() => fakeUserProfileManager.GetEnumerator()) .WillReturnCollectionValuesOf(new[] { fakeUserProfile, fakeUserProfile, fakeUserProfile });我得到一个不同的异常,是的,但仍然无法正常工作: > Method Microsoft.Office.Server.UserProfiles.ProfileManagerBase.GetEnumerator 返回 System.Collections.IEnumerator。只能在可枚举类型上使用 WillReturnCollectionValuesOf()。
  • 奇数 - 错误是使用 WillReturn 而不是 WillReturnCollectionValueOf 时创建的错误。我想这就是 Typemock 支持的用途:)
猜你喜欢
  • 1970-01-01
  • 2011-06-30
  • 1970-01-01
  • 1970-01-01
  • 2011-09-26
  • 2021-11-06
  • 1970-01-01
  • 1970-01-01
  • 2017-12-11
相关资源
最近更新 更多