Skip to content

Instantly share code, notes, and snippets.

@jenseralmeida
Last active July 15, 2019 22:57
Show Gist options
  • Save jenseralmeida/b299a11de8e5b281e7af39e83838ae4c to your computer and use it in GitHub Desktop.
Save jenseralmeida/b299a11de8e5b281e7af39e83838ae4c to your computer and use it in GitHub Desktop.
JonPSmith/EfCore.GenericServices/SingleFieldMethodMappingTests (https://github.com/JonPSmith/EfCore.GenericServices/issues/31)
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
<RootNamespace>EfCore.GenericServices.Testing</RootNamespace>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="EfCore.TestSupport" Version="2.0.0" />
<PackageReference Include="EFCore.GenericServices" Version="2.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SQLite" Version="2.2.6" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.2.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1" />
</ItemGroup>
</Project>
using System;
using System.ComponentModel;
using System.Threading.Tasks;
using GenericServices;
using GenericServices.PublicButHidden;
using GenericServices.Setup;
using Microsoft.EntityFrameworkCore;
using TestSupport.EfHelpers;
using Xunit;
namespace EfCore.GenericServices.Testing
{
public class SingleFieldMethodMappingTests
{
[Fact]
public async Task Root_method_call_fails()
{
// arrange
var options = SqliteInMemory.CreateOptions<RootContext>();
using (var context = new RootContext(options))
{
context.Database.EnsureCreated();
var utData = context.SetupSingleDtoAndEntities<SetItemDto>();
var service = new CrudServicesAsync(context, utData.ConfigAndMapper);
// act
var dto = new SetItemDto
{
Id = 1,
Item = "A item!"
};
await service.UpdateAndSaveAsync(dto);//, nameof(Root.SetItem)); // DEBUG: Exceptions is thrown. It has the same result when excplicitly setting the method name or not
}
}
public class SetItemDto : ILinkToEntity<Root>
{
[ReadOnly(true)]
public int Id { get; set; }
public string Item { get; set; }
}
public class Root
{
public int Id { get; set; }
public void SetItem(string item, RootContext context = null)
{
if (item is null)
throw new ArgumentNullException(nameof(item));
// This method is never called
}
}
public class RootContext : DbContext
{
public RootContext(DbContextOptions<RootContext> options) : base(options) { }
protected override void OnModelCreating(ModelBuilder builder)
{
builder.Entity<Root>().Property(e => e.Id).ValueGeneratedNever();
builder.Entity<Root>().HasData(new Root { Id = 1 });
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment