-
Notifications
You must be signed in to change notification settings - Fork 5
/
WebDependencyInjectionTests.cs
90 lines (81 loc) · 2.93 KB
/
WebDependencyInjectionTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
namespace RollbarDotNet.Tests.DependencyInjection
{
using System;
using System.Threading.Tasks;
using Configuration;
using Core;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Moq;
using Xunit;
public class WebDependencyInjectionTests
{
public WebDependencyInjectionTests()
{
var mockHostingEnvironment = new Mock<IHostingEnvironment>();
var services = new ServiceCollection();
services.AddOptions()
.AddRollbarWeb()
.AddSingleton(mockHostingEnvironment.Object);
services.Configure<RollbarOptions>(o =>
{
o.AccessToken = Environment.GetEnvironmentVariable("ROLLBAR_TOKEN");
o.Environment = "Testing";
});
this.Services = services;
this.ServiceProvider = this.Services.BuildServiceProvider();
this.Rollbar = this.ServiceProvider.GetService<Rollbar>();
}
protected IServiceCollection Services { get; set; }
protected IServiceProvider ServiceProvider { get; set; }
protected Rollbar Rollbar { get; set; }
[Fact]
public async Task DisabledRollbar()
{
var options = this.ServiceProvider.GetService<IOptions<RollbarOptions>>().Value;
this.Services.Configure<RollbarOptions>(o =>
{
o.AccessToken = options.AccessToken;
o.Disabled = true;
o.Environment = options.Environment;
});
var serviceProvider = this.Services.BuildServiceProvider();
var rollbar = serviceProvider.GetService<Rollbar>();
var response = await rollbar.SendMessage(RollbarLevel.Debug, "Hello");
Assert.Null(response.Result.Uuid);
}
[Fact]
public async Task SuccessfullyReportMessage()
{
var response = await this.Rollbar.SendMessage("Hello");
Assert.False(string.IsNullOrEmpty(response.Result.Uuid));
}
[Fact]
public async Task SuccessfullyReportMessageWithLevel()
{
var response = await this.Rollbar.SendMessage(RollbarLevel.Debug, "Hello");
Assert.False(string.IsNullOrEmpty(response.Result.Uuid));
}
[Fact]
public async Task SucessfullyReportError()
{
try
{
try
{
throw new Exception("Inner");
}
catch (Exception inner)
{
throw new Exception("Test", inner);
}
}
catch (Exception exception)
{
var response = await this.Rollbar.SendException(exception);
Assert.False(string.IsNullOrEmpty(response.Result.Uuid));
}
}
}
}