2016-12-14 43 views
0

我想刪除SharePoint依賴關係並將其模擬出來。默認的索引操作洛斯喜歡在新的SharePoint以下插件模板:如何單元測試SharePoint加載項?

public ActionResult Index() 
    { 
     User spUser = null; 
     var spContext = SharePointContextProvider.Current.GetSharePointContext(HttpContext); 
     using (var clientContext = spContext.CreateUserClientContextForSPHost()) 
     { 
      if (clientContext != null) 
      { 
       spUser = clientContext.Web.CurrentUser; 
       clientContext.Load(spUser, user => user.Title); 
       clientContext.ExecuteQuery(); 
       ViewBag.UserName = spUser.Title; 
      } 
     } 
     return View(); 
    } 

我試圖打包ClientContext插入適配器,但不能模擬出的網絡媒體:

public interface IClientContext 
{ 
    Web Web { get; } 
    void Load<T>(T clientObject, params Expression<Func<T, object>>[] retrievals) where T : ClientObject; 
    void ExecuteQuery(); 
} 

public class ClientContextAdapter : IClientContext 
{ 
    private readonly ClientContext _wrappedClient; 
    public ClientContextAdapter(ClientContext client) 
    { 
     _wrappedClient = client; 
    } 

    public Web Web => _wrappedClient.Web; 

    public void Load<T>(T clientObject, params Expression<Func<T, object>>[] retrievals) where T : ClientObject 
    { 
     _wrappedClient.Load(clientObject, retrievals); 
    } 
    public void ExecuteQuery() 
    { 
     _wrappedClient.ExecuteQuery(); 
    } 
} 

如何你是否測試你的SharePoint加載項?

回答

2

找到了一個解決方案,因此SharePoint依賴可以通過以下方式嘲笑。網站應裝入適配器以及:

public interface IClientContext 
{ 
    IWeb Web { get; } 
    void Load<T>(T clientObject, params Expression<Func<T, object>>[] retrievals) where T : ClientObject; 
    void ExecuteQuery(); 
} 

public class ClientContextAdapter : IClientContext 
{ 
    private readonly ClientContext _wrappedClient; 
    public ClientContextAdapter(ClientContext client) 
    { 
     _wrappedClient = client; 
    } 

    public IWeb Web => new WebAdapter(_wrappedClient.Web); 

    public void Load<T>(T clientObject, params Expression<Func<T, object>>[] retrievals) where T : ClientObject 
    { 
     _wrappedClient.Load(clientObject, retrievals); 
    } 

    public void ExecuteQuery() 
    { 
     _wrappedClient.ExecuteQuery(); 
    } 
} 

ClientContext依賴從HomeController中有窮人的依賴注入去除:

 IClientContext _clientContext; 

    #region Constructors 
    public HomeController() 
    { 
     // Called by MVC 
    } 

    public HomeController(IClientContext clientContext) 
    { 
     _clientContext = clientContext; 
    } 
    #endregion 

    [SharePointContextFilter] 
    public ActionResult Index() 
    { 
     if (_clientContext == null) 
     { 
      var spContext = SharePointContextProvider.Current.GetSharePointContext(HttpContext); 
      _clientContext = new ClientContextAdapter(spContext.CreateUserClientContextForSPHost()); 
     } 

     if (_clientContext != null) 
     { 
      User spUser = _clientContext.Web.CurrentUser; 
      _clientContext.Load(spUser, user => user.Title); 
      _clientContext.ExecuteQuery(); 
      ViewBag.UserName = spUser.Title; 
     } 

     return View(); 
    } 

的裏面網絡適配器

public interface IWeb 
{ 
    User CurrentUser { get; } 
} 

public class WebAdapter : IWeb 
{ 
    private readonly Web _wrappedClient; 
    public WebAdapter(Web client) 
    { 
     _wrappedClient = client; 
    } 

    public User CurrentUser => _wrappedClient.CurrentUser; 
} 

ClientContext適配器指數行爲與Moq和FluentAssertions的單元測試:

[TestClass] 
class HomeControllerTests 
{ 
    private HomeController _homeController; 

    [TestInitialize] 
    public void Init() 
    { 
     // Arrange 
     var user = new User(new ClientContext("http://localhost"), 
      new ObjectPathConstructor(new ClientContext("http://localhost"), string.Empty, null)); 
     user.Title = "TestUser"; 
     var mockWeb = new Mock<IWeb>(); 
     mockWeb.SetupGet(w => w.CurrentUser).Returns(user); 
     var mockClient = new Mock<IClientContext>(); 
     mockClient.SetupGet(c => c.Web).Returns(mockWeb.Object); 

     _homeController = new HomeController(mockClient.Object); 
    } 

    [TestMethod] 
    public void Index() 
    { 
     // Act 
     var result = (ViewResult)_homeController.Index(); 

     // Assert 
     result.Should().BeViewResult(); 
     string userName = result.ViewBag.UserName; 
     userName.Should().Be("TestUser"); 
    } 
}