Skip to content

Commit

Permalink
Use moq
Browse files Browse the repository at this point in the history
  • Loading branch information
Wayne-KTCSZ committed Jun 13, 2024
1 parent 3c769e6 commit 3a5d395
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 0 deletions.
67 changes: 67 additions & 0 deletions test/ZKEACMS.Test/Account/UserCenterLinkTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/* http://www.zkea.net/
* Copyright (c) ZKEASOFT. All rights reserved.
* http://www.zkea.net/licenses */

using Microsoft.Extensions.DependencyInjection;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZKEACMS.Account;

namespace ZKEACMS.Test.Account
{
[TestClass]
public class UserCenterLinkTest
{
private readonly IServiceProvider _serviceProvider;
public UserCenterLinkTest()
{
var serviceCollection = new ServiceCollection();
serviceCollection.AddTransient<IUserCenterLinkService, UserCenterLinkService>();

serviceCollection.AddTransient(provider =>
{
var userCenterLinksProvider = new Mock<IUserCenterLinksProvider>();
userCenterLinksProvider.Setup(m => m.GetLinks()).Returns(new List<AdminMenu>
{
new AdminMenu
{
Title="Menu1"
},
new AdminMenu
{
Title="Menu2"
}
});
return userCenterLinksProvider.Object;
});
serviceCollection.AddTransient(provider =>
{
var userCenterLinksProvider = new Mock<IUserCenterLinksProvider>();
userCenterLinksProvider.Setup(m => m.GetLinks()).Returns(new List<AdminMenu>
{
new AdminMenu
{
Title="Menu3"
},
new AdminMenu
{
Title="Menu4"
}
});
return userCenterLinksProvider.Object;
});
_serviceProvider = serviceCollection.BuildServiceProvider();
}
[TestMethod]
public void TestGetLinks()
{
var links = _serviceProvider.GetService<IUserCenterLinkService>().GetLinks();
Assert.IsTrue(links.Count() == 4);
}
}
}
45 changes: 45 additions & 0 deletions test/ZKEACMS.Test/Event/EventTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/* http://www.zkea.net/
* Copyright (c) ZKEASOFT. All rights reserved.
* http://www.zkea.net/licenses */

using Microsoft.Extensions.DependencyInjection;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZKEACMS.Event;

namespace ZKEACMS.Test.Event
{
[TestClass]
public class EventTest
{
private readonly IServiceProvider _serviceProvider;
public EventTest()
{
var serviceCollection = new ServiceCollection();
serviceCollection.AddTransient<IEventManager, EventManager>();

serviceCollection.RegistEvent<TestEventHandler>("Test");
_serviceProvider = serviceCollection.BuildServiceProvider();
}
[TestMethod]
public void TestTrigger()
{
_serviceProvider.GetService<IEventManager>().Trigger("Test", new object());
_serviceProvider.GetService<IEventManager>().Trigger(new EventArg { Name = "Test" }, new object());
}
}
public class TestEventHandler : IEventHandler
{
public void Handle(object entity, EventArg e)
{
Assert.IsNotNull(entity);
Assert.IsNotNull(e);
Assert.IsTrue(e.Name == "Test");
}
}
}
1 change: 1 addition & 0 deletions test/ZKEACMS.Test/ZKEACMS.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageReference Include="Moq" Version="4.20.70" />
<PackageReference Include="MSTest.TestAdapter" Version="3.4.3" />
<PackageReference Include="MSTest.TestFramework" Version="3.4.3" />
<PackageReference Include="Selenium.Chrome.WebDriver" Version="85.0.0" />
Expand Down

0 comments on commit 3a5d395

Please sign in to comment.