public void HandleNotificationAsync_JoinedGuildNotification_NoChannelsAreTextChannel_CompletesImmediately(params Type[] channelTypes) { var autoMocker = new AutoMocker(); var uut = autoMocker.CreateInstance <ChannelTrackingBehavior>(); var mockChannels = channelTypes .Select(x => typeof(Mock <>).MakeGenericType(x).GetConstructor(Array.Empty <Type>()).Invoke(Array.Empty <object>()) as Mock) .ToArray(); var mockGuild = new Mock <ISocketGuild>(); mockGuild .Setup(x => x.Channels) .Returns(mockChannels .Select(x => x.Object as ISocketGuildChannel) .ToArray()); mockGuild .Setup(x => x.Available) .Returns(true); var notification = new JoinedGuildNotification(mockGuild.Object); using (var cancellationTokenSource = new CancellationTokenSource()) { uut.HandleNotificationAsync(notification, cancellationTokenSource.Token) .IsCompletedSuccessfully.ShouldBeTrue(); autoMocker.GetMock <IChannelService>() .Invocations.ShouldBeEmpty(); } }
public static TestCaseData BuildTestCaseData_HandleNotificationAsync_JoinedGuildNotification( IReadOnlyCollection <ulong> roleIds) { var roles = roleIds .Select(roleId => { var mockRole = new Mock <ISocketRole>(); mockRole .Setup(x => x.Id) .Returns(roleId); return(mockRole.Object); }) .ToArray(); var mockGuild = new Mock <ISocketGuild>(); mockGuild .Setup(x => x.Roles) .Returns(roles); var notification = new JoinedGuildNotification( mockGuild.Object); return(new TestCaseData(notification)); }
public void Constructor_Always_PropertiesAreGiven() { var mockGuild = new Mock <ISocketGuild>(); var uut = new JoinedGuildNotification(mockGuild.Object); uut.Guild.ShouldBeSameAs(mockGuild.Object); }
public async Task HandleNotificationAsync_JoinedGuildNotification_AnyChannelIsTextChannel_TracksTextChannels(params Type[] channelTypes) { var autoMocker = new AutoMocker(); var uut = autoMocker.CreateInstance <ChannelTrackingBehavior>(); var mockChannels = channelTypes .Select(x => typeof(Mock <>).MakeGenericType(x).GetConstructor(Array.Empty <Type>()).Invoke(Array.Empty <object>()) as Mock) .ToArray(); var mockGuild = new Mock <ISocketGuild>(); mockGuild .Setup(x => x.Channels) .Returns(mockChannels .Select(x => x.Object as ISocketGuildChannel) .ToArray()); mockGuild .Setup(x => x.Available) .Returns(true); var notification = new JoinedGuildNotification(mockGuild.Object); using (var cancellationTokenSource = new CancellationTokenSource()) { await uut.HandleNotificationAsync(notification, cancellationTokenSource.Token); var mockChannelService = autoMocker.GetMock <IChannelService>(); var textChannels = mockChannels .Select(x => x.Object) .OfType <ISocketTextChannel>() .ToArray(); foreach (var textChannel in textChannels) { mockChannelService.ShouldHaveReceived(x => x.TrackChannelAsync(textChannel, cancellationTokenSource.Token)); } mockChannelService .Invocations .Where(x => x.Method.Name == nameof(IChannelService.TrackChannelAsync)) .Select(x => x.Arguments[0]) .ShouldBe(textChannels, ignoreOrder: true); } }
public async Task HandleNotificationAsync_JoinedGuildNotification_Always_TracksRoles( JoinedGuildNotification notification) { using var testContext = new TestContext(); var uut = testContext.BuildUut(); await uut.HandleNotificationAsync( notification, testContext.CancellationToken); foreach (var role in notification.Guild.Roles) { testContext.MockRoleService.ShouldHaveReceived(x => x .TrackRoleAsync(role, testContext.CancellationToken)); } testContext.MockRoleService.ShouldHaveReceived(x => x .TrackRoleAsync(It.IsAny <IRole>(), It.IsAny <CancellationToken>()), Times.Exactly(notification.Guild.Roles.Count)); }
public async Task HandleNotificationAsync_JoinedGuildNotification_GuildIsAvailable_InvokesAutoConfigureGuildAsync() { var autoMocker = new AutoMocker(); var uut = autoMocker.CreateInstance <AuthorizationAutoConfigBehavior>(); var mockGuild = autoMocker.GetMock <ISocketGuild>(); mockGuild .Setup(x => x.Available) .Returns(true); using (var cancellationTokenSource = new CancellationTokenSource()) { var notification = new JoinedGuildNotification(mockGuild.Object); await uut.HandleNotificationAsync(notification, cancellationTokenSource.Token); autoMocker.GetMock <IAuthorizationService>() .ShouldHaveReceived(x => x.AutoConfigureGuildAsync(mockGuild.Object, cancellationTokenSource.Token)); } }
public void HandleNotificationAsync_JoinedGuildNotification_GuildIsNotAvailable_CompletesImmediately() { var autoMocker = new AutoMocker(); var uut = autoMocker.CreateInstance <AuthorizationAutoConfigBehavior>(); var mockGuild = autoMocker.GetMock <ISocketGuild>(); mockGuild .Setup(x => x.Available) .Returns(false); using (var cancellationTokenSource = new CancellationTokenSource()) { var notification = new JoinedGuildNotification(mockGuild.Object); uut.HandleNotificationAsync(notification, cancellationTokenSource.Token) .IsCompleted.ShouldBeTrue(); autoMocker.GetMock <IAuthorizationService>() .ShouldNotHaveReceived(x => x.AutoConfigureGuildAsync(It.IsAny <IGuild>(), It.IsAny <CancellationToken>())); } }
/// <inheritdoc /> public Task HandleNotificationAsync(JoinedGuildNotification notification, CancellationToken cancellationToken = default) => notification.Guild.Available ? _authorizationService.AutoConfigureGuildAsync(notification.Guild, cancellationToken) : Task.CompletedTask;