Morning,
I'm testing model validation in NancyFX (1.4.1). The validation works fine when hitting the API endpoint, but fails in the tests. I'm using the default bootstrapper. The tests are in a separate project. Both projects have nancy.validation.fluentvalidation package referenced. Is there some more configuration required in the Test browser?
Thanks in advance!
Model:
public class CreateServiceCommand
{
[Required(AllowEmptyStrings = false)]
public string TestField { get; set; }
}
Module under test:
public class ServiceModule : NancyModule
{
private readonly IServiceCreateRequestedListener _listener;
public ServiceModule(IServiceCreateRequestedListener listener)
{
_listener = listener;
Post["/services/create"] = parameters =>
{
var request = this.Bind<CreateServiceCommand>();
var result = this.Validate(request);
if(!result.IsValid) return HttpStatusCode.BadRequest;
_listener.CreateServiceRequested(request);
return "";
};
}
}
Test snippet:
[SetUp]
public void Setup()
{
var browser = new Browser(with =>
{
with.Module<ServiceModule>();
with.Dependency<IServiceCreateRequestedListener>(this);
});
_result = browser.Post("/services/create", with =>
{
with.HttpRequest();
});
}
[Test]
public void ShouldReturnBadRequest
{
Assert.That(_result.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
}
The data annotation on the CreateServiceCommand as shown in the demo code didn't work.
I had to implement an instance of
AbstractValidator<CreateServiceCommand>
from FluentValidation
Related
SonarQube ignores tests for AbstractValidators (FluentValidation.AspNetCore nuget package). In the coverage section, all RuleFor lines are marked as not covered.
What do I need to do, for the tests to be included in the coverage in SonarQube?
ReSharper shows the validator as being fully covered by tests.
The validator:
public class UserValidator : AbstractValidator<User>
{
public UserValidator()
{
RuleFor(a => a.Name).NotEmpty();
RuleFor(a => a.FirstName).NotEmpty();
}
}
Test class:
public class UserValidatorTests
{
[Test, TestCaseSource(nameof(User_InvalidData))]
public async Task ValidationShouldFail(UserAndErrors testData)
{
var result = await new UserValidator().TestValidateAsync(testData.User!);
Assert.IsFalse(result.IsValid);
Assert.AreEqual(result.Errors.Count, testData.ErrorsCount);
}
[Test]
public async Task ValidationShouldPass()
{
var request = new User
{
FirstName = "John",
Name= "Doe"
};
var result = await new UserValidator().TestValidateAsync(request);
Assert.IsTrue(result.IsValid);
result.ShouldNotHaveAnyValidationErrors();
}
private static IEnumerable<TestCaseData> User_InvalidData()
{
yield return new TestCaseData(new User
{
User = new User(),
ErrorsCount = 2
});
yield return new TestCaseData(new User
{
User = new User
{
Name = "Doe"
},
ErrorsCount = 1
});
yield return new TestCaseData(new User
{
User = new User
{
FirstName = "John"
},
ErrorsCount = 1
});
}
public class UserAndErrors
{
public User? User { get; set; }
public int ErrorsCount { get; set; } = 0;
}
}
User class:
public class User
{
public string Name { get; set; } = string.Empty;
public string FirstName { get; set; } = string.Empty;
}
The issue was the fact that the test project did not reference the coverlet.msbuild nuget package. Without it, SonarQube could not run the tests correctly and could not update the code coverage.
I am trying to make a console app that uses a custom AppService by adapting from the example https://github.com/aspnetboilerplate/aspnetboilerplate-samples/blob/master/AbpEfConsoleApp/AbpEfConsoleApp/Program.cs
It works for me to call the service, but when I try to use a IRepository gives me the following error
Castle.MicroKernel.Handlers.HandlerException: 'Can't create component 'VCloud.Rtdm.CashAudit.TestManager' as it has dependencies to be satisfied.
It's as if I didn't have the IRepository registered.
Program.cs
class Program
{
static void Main(string[] args)
{
TestUserReferencedService();
}
/// <summary>
/// Prueba de USAR un servicio de aspnetzero referenciando al proyecto. Spoiler: No funciona
/// </summary>
static async void TestUserReferencedService()
{
Clock.Provider = ClockProviders.Utc;
Console.WriteLine("Starting");
//Bootstrapping ABP system
using (var bootstrapper = AbpBootstrapper.Create<VCloudConsoleApplicationModule>())
{
bootstrapper.IocManager
.IocContainer
.AddFacility<LoggingFacility>(f => f.UseAbpLog4Net().WithConfig("log4net.config"));
bootstrapper.Initialize();
//Getting a Tester object from DI and running it
using (var tester = bootstrapper.IocManager.ResolveAsDisposable<TestAppService>())
{
var x = (await tester.Object.TestCount());
} //Disposes tester and all it's dependencies
Console.WriteLine("Press enter to exit...");
Console.ReadLine();
}
}
}
VCloudConsoleApplicationModule.cs
[DependsOn(
typeof(VCloudApplicationSharedModule),
typeof(VCloudConsoleCoreModule),
typeof(AbpEntityFrameworkCoreModule),
typeof(AbpDapperModule),
typeof(AbpZeroCommonModule)
)]
public class VCloudConsoleApplicationModule : AbpModule
{
public override void PreInitialize()
{
//Adding authorization providers
Configuration.Authorization.Providers.Add<AppAuthorizationProvider>();
//Adding custom AutoMapper configuration
Configuration.Modules.AbpAutoMapper().Configurators.Add(CustomDtoMapper.CreateMappings);
Configuration.BackgroundJobs.IsJobExecutionEnabled = false;
}
public override void Initialize()
{
IocManager.RegisterAssemblyByConvention(typeof(VCloudConsoleApplicationModule).GetAssembly());
DapperExtensions.DapperExtensions.SetMappingAssemblies(new List<Assembly>
{typeof(VCloudConsoleApplicationModule).GetAssembly()});
DapperExtensions.DapperExtensions.SetMappingAssemblies(new List<Assembly>
{
typeof(VCloud.Dapper.Mappers.DapperMapper_RtdmOrder).GetAssembly(),
typeof(VCloud.Dapper.Mappers.DapperMapper_RtdmOrderItem).GetAssembly(),
typeof(VCloud.Dapper.Mappers.DapperMapper_RtdmCompany).GetAssembly(),
typeof(VCloud.Dapper.Mappers.DapperMapper_RtdmRestaurant).GetAssembly()
});
//DapperExtensions.DapperExtensions.Configure();
//Dapper.DefaultTypeMap.MatchNamesWithUnderscores = true;
}
}
TestManager.cs
public class TestManager : VCloudDomainServiceBase, ITestManager
{
private IRepository<Invoice> _repository;
public TestManager(IRepository<Invoice> repository)
{
_repository = repository;
}
public async Task<int> GetCount()
{
return await _repository.CountAsync();
}
}
Your problem is not related to Dependeny Injection, the problem may be you forget to add the entity Invoice to the DbContext of your application in EntityFramework project.
Once you add it your problem will be solved.
public class VCloudApplicationDbContext : AbpZeroDbContext<Tenant, Role, User, VCloudApplicationDbContext>, IAbpPersistedGrantDbContext
{
public DbSet<Invoice> Invoice { get; set; }
}
I hope it could help you
I have an application that needs to intercept the current message consume context and extract a value that is defined in a base interface. That value is a tenant code that is eventually used in an EF database context.
I have a provider that takes a MassTransit ConsumerContext, and then using context.TryGetMessage(), extracts the tenant code, which is ultimately used to switch database contexts to a specific tenant database.
The issue lies in the MessageContextTenantProvider below. If a non-fault message is consumed then ConsumeContext<IBaseEvent> works fine. However if it is a fault, ConsumeContext<Fault<IBaseEvent>> doesn't work as expected.
Durring debugging I can see that the message context for a fault is ConsumeContext<Fault<IVerifyEvent>>, but why doesn't it work with a base interface as per the standard message? Of course, ConsumeContext<Fault<IVerifiedEvent>> works fine, but I have a lot of message types, and I don't want to have to define them all in that tenant provider.
Any ideas?
public interface ITenantProvider
{
string GetTenantCode();
}
public class MessageContextTenantProvider : ITenantProvider
{
private readonly ConsumeContext _consumeContext;
public MessageContextTenantProvider(ConsumeContext consumeContext)
{
_consumeContext = consumeContext;
}
public string GetTenantCode()
{
// get tenant from message context
if (_consumeContext.TryGetMessage(out ConsumeContext<IBaseEvent> baseEvent))
{
return baseEvent.Message.TenantCode; // <-- works for the non fault consumers
}
// get tenant from fault message context
if (_consumeContext.TryGetMessage<Fault<IBaseEvent>>(out var gebericFaultEvent))
{
return gebericFaultEvent.Message.Message.TenantCode; // <- doesn't work generically
}
// get tenant from fault message context (same as above)
if (_consumeContext.TryGetMessage(out ConsumeContext<Fault<IBaseEvent>> faultEvent))
{
return faultEvent.Message.Message.TenantCode; // <= generically doesn't work when using the base interface?
}
// get tenant from specific concrete fault class
if (_consumeContext.TryGetMessage(out ConsumeContext<Fault<IVerifiedEvent>> verifiedFaultEvent))
{
return verifiedFaultEvent.Message.Message.TenantCode; // <-- this works
}
// not able to extract tenant
return null;
}
}
public partial class VerificationDbContext
{
string connectionString;
public string ConnectionString
{
get
{
if (connectionString == null)
{
string tenantCode = _tenantProvider.GetTenantCode();
connectionString = _tenantConnectionManager.GetConnectionString(orgId);
}
return connectionString;
}
}
private readonly ITenantProvider _tenantProvider;
private readonly ITenantConnectionManager _tenantConnectionManager;
public VerificationDbContext(ITenantProvider tenantProvider, ITenantConnectionManager tenantConnectionManager)
{
_tenantProvider = tenantProvider;
_tenantConnectionManager = tenantConnectionManager;
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (string.IsNullOrEmpty(this.ConnectionString))
{
optionsBuilder.UseSqlServer(#"Data Source=.\SQLEXPRESS;Initial Catalog=VerificationDb;Integrated Security=True")
.ConfigureWarnings((warningBuilder) => warningBuilder.Ignore(RelationalEventId.AmbientTransactionWarning));
}
else
{
optionsBuilder.UseSqlServer(this.ConnectionString)
.ConfigureWarnings((warningBuilder) => warningBuilder.Ignore(RelationalEventId.AmbientTransactionWarning));
}
}
}
public interface ITenantConnectionManager
{
string GetConnectionString(string tenantCode);
}
public class TenantConnectionManager : ITenantConnectionManager
{
private ITenantRepository _tenantRepository;
public TenantConnectionManager(ITenantRepository tenantRepository)
{
_tenantRepository = tenantRepository;
}
public string GetConnectionString(string tenantCode)
{
return _tenantRepository.GetByTenantCode(tenantCode).ConnectionString;
}
}
public interface IBaseEvent
{
string TenantCode { get; }
}
public interface IVerifiedEvent : IBaseEvent
{
string JobReference { get; }
}
public class VerifiedEventConsumer : IConsumer<IVerifiedEvent>
{
private readonly IVerifyCommand _verifyCommand;
private readonly ITenantProvider _tenantProvider;
public VerifiedEventConsumer(ITenantProvider tenantProvider, IVerifyCommand verifyCommand)
{
_verifyCommand = verifyCommand;
_tenantProvider = tenantProvider;
}
public async Task Consume(ConsumeContext<IVerifiedEvent> context)
{
await _verifyCommand.Execute(new VerifyRequest
{
JobReference = context.Message.JobReference,
TenantCode = context.Message.TenantCode
});
}
}
public class VerifiedEventFaultConsumer : IConsumer<Fault<IVerifiedEvent>>
{
private readonly IVerifyFaultCommand _verifyFaultCommand;
private readonly ITenantProvider _tenantProvider;
public CaseVerifiedEventFaultConsumer(ITenantProvider tenantProvider, IVerifyFaultCommand verifyFaultCommand)
{
_verifyFaultCommand = verifyFaultCommand;
_tenantProvider = tenantProvider;
}
public async Task Consume(ConsumeContext<Fault<ICaseVerifiedEvent>> context)
{
await _verifyFaultCommand.Execute(new VerifiedFaultRequest
{
JobReference = context.Message.Message.JobReference,
Exceptions = context.Message.Exceptions
});
}
}
I've solved the issue by using the GreenPipes TryGetPayload extension method:
public class MessageContextTenantProvider : ITenantProvider
{
private readonly ConsumeContext _consumeContext;
public MessageContextTenantProvider(ConsumeContext consumeContext)
{
_consumeContext = consumeContext;
}
public string GetTenantCode()
{
// get tenant from message context
if (_consumeContext.TryGetMessage(out ConsumeContext<IBaseEvent> baseEvent))
{
return baseEvent.Message.TenantCode;
}
// get account code from fault message context using Greenpipes
if (_consumeContext.TryGetPayload(out ConsumeContext<Fault<IBaseEvent>> payloadFaultEvent))
{
return payloadFaultEvent.Message.Message.TenantCode;
}
// not able to extract tenant
return null;
}
}
I want to write a cross mobile platform app that sets up the alarm by specifying the required parameters like Date and Time. I just want to set up only one time and not repeatedly.
I was unable to find any readily available plugin in mvvmcross or in Xamarin ?
Please help
Since there is no existing plugin within MVVMCross, you may want to write your own plugin. You can find the documentation here:
https://github.com/MvvmCross/MvvmCross/wiki/MvvmCross-plugins
Because you'd like to specify a few parameters, you'd want to see the following section:
https://github.com/MvvmCross/MvvmCross/wiki/MvvmCross-plugins#writing-a-configurable-plugin
Overall this is what you might do:
General Interface
public interface IAlarm
{
void SetupAlarm();
}
public class PluginLoader
: IMvxPluginLoader
{
public static readonly PluginLoader Instance = new PluginLoader();
public void EnsureLoaded()
{
var manager = Mvx.Resolve<IMvxPluginManager>();
manager.EnsurePlatformAdaptionLoaded<PluginLoader>();
}
}
Android Implementation
public class DroidAlarmConfiguration
: IMvxPluginConfiguration
{
public AlarmLength { get; set;}
}
public class DroidAlarm : IAlarm
{
public TimeSpan AlarmLength { get; set; }
public void SetupAlarm()
{
//ALARM IMPLEMENTATION HERE. NOTE THIS IS SOME JAVA SYNTAX!!!!
var globals = Mvx.Resolve<Cirrious.CrossCore.Droid.IMvxAndroidGlobals>();
var alarm = globals.ApplicationContext
.GetSystemService(Context.ALARM_SERVICE)
as AlarmManager;
Intent intent = new Intent(context, AlarmReceiver.class);
alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
alarm.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,
alarmLength, alarmIntent);
}
}
public class Plugin
: IMvxPlugin
{
private _alarmLength = **Your Value Here**;
public void Configure(IMvxPluginConfiguration configuration)
{
if (configuration == null)
return;
var droidConfiguration = (DroidAlarmConfiguration)configuration;
_alarmLength = droidConfiguration.AlarmLength;
}
public void Load()
{
var instance = new DroidAlarm();
instance.AlarmLength = _AlarmLength;
Mvx.RegisterSingleton<IAlarm>(instance);
}
}
Setup.cs - To set the values in one core place for all android/ios/windows
protected override IMvxPluginConfiguration GetPluginConfiguration(Type plugin)
{
if (plugin == typeof(Yours.Alarm.Droid.Plugin))
{
return new Yours.Alarm.Droid.DroidAlarmConfiguration()
{
AlarmLength = **YOUR VALUE HERE**
};
}
return null;
}
You would then follow the same Droid step for iOS and Windows Phone. I hope this helps!
I am trying to arrange the input parameter of the lambda that is passed to ICallback#Regsiter<T>(Action<T>) in the (paired down) unit test sample below (see the comments in the unit test sample). I am trying to avoid having to abstract out the lambda because it's so specific and small, but I'm not sure if that's possible.
// IBus interface peek
public interface IBus {
ICallback Send(IMessage message);
}
// ICallback interface peek
public interface ICallback {
void Register<T>(Action<T> callback);
}
public enum ReturnCode { Success }
// Controller
public class FooController : AsyncController {
readonly IBus _bus;
//...
// Action being unit tested
public void BarAsync() {
_bus
.Send(ZapMessageFactory.Create())
.Register<ReturnCode>(x => {
AsyncManger.Parameters["returnCode"] = x;
});
}
public ActionResult BarCompleted(ReturnCode returnCode) {
// ...
}
}
// Controller action unit test
[TestClass]
public class FooControllerTest {
[TestMethod}
public void BarTestCanSetAsyncManagerParameterErrorCodeToSuccess() {
var fooController = ControllerUTFactory.CreateFooController();
// HOW DO I MOCK THE ACTION DELEGATE PARAMETER TO BE ReturnCode.Success
// SO I CAN DO THE ASSERT BELOW???
fooController.BarAsync();
Assert.AreEqual(ReturnCode.Success, (ReturnCode)fooController.AsyncManager.Parameters["returnCode"]);
}
}
Using the Mock<T>#Callback() is the answer:
[TestMethod}
public void BarTestCanSetAsyncManagerParameterErrorCodeToSuccess() {
var mockCallback = new Mock<ICallback>();
mockCallback
.Setup(x => x.Register(It.IsAny<ReasonCode>())
// THIS LINE IS THE ANSWER
.Callback(action => action(ReasonCode.Success));
var mockBus = new Mock<IBus>();
mockBus
.Setup(x => x.Send(It.IsAny<ZapMessage>())
.Returns(mockCallback.Object);
var fooController = new FooController(mockBus.Object);
fooController.BarAsync();
Assert.AreEqual(ReturnCode.Success, (ReturnCode)fooController.AsyncManager.Parameters["returnCode"]);
}