Assume that I have the following class structure:
public class Outer
{
[Dependency]
public Func<Inner> InnerFactory { get; set; }
}
public class Inner
{
}
In Autofac this can be done easily in the following way (and describes the behaviour I am looking for; ignore the fact that this is a stupid contrived example):
[TestMethod]
public void AutofacMakesThisEasy()
{
var builder = new ContainerBuilder();
builder.RegisterType<Outer>().PropertiesAutowired();
builder.RegisterType<Inner>().InstancePerOwned<Outer>();
var container = builder.Build();
var outer1 = container.Resolve<Owned<Outer>>().Value;
var inner1 = outer1.InnerFactory();
var inner2 = outer1.InnerFactory();
var outer2 = container.Resolve<Owned<Outer>>().Value;
var inner3 = outer2.InnerFactory();
var inner4 = outer2.InnerFactory();
Assert.AreNotSame(outer1, outer2, "outer1 == outer2");
Assert.AreSame(inner1, inner2, "inner1 != inner2");
Assert.AreNotSame(inner2, inner3, "inner2 == inner3");
Assert.AreSame(inner3, inner4, "inner3 != inner4");
}
How can I achieve the same behaviour in unity so the following test passes?
[TestMethod]
public void UnityHasMeScratchingMyHead()
{
var container = new UnityContainer();
//// What shoud I do here?
var outer1 = container.Resolve<Outer>();
var inner1 = outer1.InnerFactory();
var inner2 = outer1.InnerFactory();
var outer2 = container.Resolve<Outer>();
var inner3 = outer2.InnerFactory();
var inner4 = outer2.InnerFactory();
Assert.AreNotSame(outer1, outer2, "outer1 == outer2");
Assert.AreSame(inner1, inner2, "inner1 != inner2");
Assert.AreNotSame(inner2, inner3, "inner2 == inner3");
Assert.AreSame(inner3, inner4, "inner3 != inner4");
}
Note: I would dearly love to ditch Unity for a decent IoC, but that is going to be a very hard sell.
If InnerFactory was just Inner instead of Func<Inner> The solution would be PerResolveLifetimeManager
[TestMethod]
public void UnityHasMeScratchingMyHead()
{
var container = new UnityContainer();
container.RegisterType<Inner>(new PerResolveLifetimeManager());
var outer1 = container.Resolve<Outer>();
//... and so on.
However because you have a factory you need to make it a little more complicated. You need to make the factory PerResolve then have the objects the factory makes all the same instance (ContainerControlledLifetimeManager). You do that via the following:
[TestMethod]
public void UnityHasMeScratchingMyHead()
{
var container = new UnityContainer();
container.RegisterType<Func<Inner>>(new PerResolveLifetimeManager(), new InjectionFactory(x =>
{
var child = x.CreateChildContainer();
child.RegisterType<Inner>(new ContainerControlledLifetimeManager());
return new Func<Inner>(() => child.Resolve<Inner>());
}));
var outer1 = container.Resolve<Outer>();
var inner1 = outer1.InnerFactory();
var inner2 = outer1.InnerFactory();
var outer2 = container.Resolve<Outer>();
var inner3 = outer2.InnerFactory();
var inner4 = outer2.InnerFactory();
Assert.AreNotSame(outer1, outer2, "outer1 == outer2");
Assert.AreSame(inner1, inner2, "inner1 != inner2");
Assert.AreNotSame(inner2, inner3, "inner2 == inner3");
Assert.AreSame(inner3, inner4, "inner3 != inner4");
}
One thing of note, Unity is easily extensible. If you are going to be doing this frequently with a little work you could make a InstancePerOwnedLifetimeManager<T> and just use that and not need to worry about generating factories at all.
Related
How can I make an class instance manually of a class that requires an IOptionsMonitor in the constructor?
My Class
private readonly AuthenticationSettings _authenticationSettings;
public ActiveDirectoryLogic(IOptionsMonitor<AuthenticationSettings> authenticationSettings)
{
_authenticationSettings = authenticationSettings.CurrentValue;
}
My test
AuthenticationSettings au = new AuthenticationSettings(){ ... };
var someOptions = Options.Create(new AuthenticationSettings());
var optionMan = new OptionsMonitor(someOptions); // dont work.
ActiveDirectoryLogic _SUT = new ActiveDirectoryLogic(au);
I tried to make an IOptionsMonitor object manually but can't figure out how.
You are calling the constructor of the OptionsMonitor<TOptions> class incorrectly.
In this case I would have just mocked the IOptionsMonitor<AuthenticationSettings> interface
For example using Moq
AuthenticationSettings au = new AuthenticationSettings() { ... };
var monitor = Mock.Of<IOptionsMonitor<AuthenticationSettings>>(_ => _.CurrentValue == au);
ActiveDirectoryLogic _SUT = new ActiveDirectoryLogic(monitor);
Here is another way to do it that doesn't involve trying to set the readonly CurrentValue field.
using Moq;
private IOptionsMonitor<AppConfig> GetOptionsMonitor(AppConfig appConfig)
{
var optionsMonitorMock = new Mock<IOptionsMonitor<AppConfig>>();
optionsMonitorMock.Setup(o => o.CurrentValue).Returns(appConfig);
return optionsMonitorMock.Object;
}
Achieving the same in NSubstitute:
var optionsMonitorMock = Substitute.For<IOptionsMonitor<AuthenticationSettings>>();
optionsMonitorMock.CurrentValue.Returns(new AuthenticationSettings
{
// values go here
});
I have the code below which I would like to test, but I'm not sure whether it is possible or not.
I have EF repositories and they are put together to a class as public properties. I don't know exactly whether it is bad solution or not, but it is easier to manage the code and its dependencies. Only the testability is still a question.
Purpose of my test is injecting data via
administrationRepository.ModuleScreen.GetAll()
method and catch the result. I know that it can be tested once it is deployed, but I want the tests in build time in order to have as fast feedback as possible.
I went through questions and answers here, but I cannot find answers. In my code I got to the point where the property is set up, but when I call the administrationRepoMock.Object.ModuleScreen.GetAll() ReSharper offers only the methods coming from Entitiy Framework and not the Moq related functions.
It is possible what I want? If so, how? Is my design suitable for this? If not can you give me articles, urls where I can see examples?
Repository:
public interface IModuleScreen
{
IEnumerable<DomainModel.Administration.ModuleScreen> GetAll();
}
public interface IAdministrationRepository
{
IModuleScreen ModuleScreen { get; }
}
public partial class AdministrationRepository : IAdministrationRepository
{
public virtual IModuleScreen ModuleScreen { get; private set; }
public AdministrationRepository( IModuleScreen moduleScreen )
{
this.ModuleScreen = moduleScreen;
}
}
Application:
public partial class DigitalLibraryApplication : IDigitalLibraryApplication
{
private IAdministrationRepository _administrationRepository;
private IMapper.IMapper.IMapper _mapper;
private IDiLibApplicationHelper _dilibApplicationHelper;
#region Ctor
public DigitalLibraryApplication( IAdministrationRepository administrationRepository, IMapper.IMapper.IMapper mapper, IDiLibApplicationHelper diLibApplicationHelper)
{
_administrationRepository = administrationRepository;
_mapper = mapper;
_dilibApplicationHelper = diLibApplicationHelper;
}
#endregion
public IEnumerable<ModuleScreenContract> GetModuleScreens()
{
//inject data here
IEnumerable<ModuleScreen> result = _administrationRepository.ModuleScreen.GetAll();
List<ModuleScreenContract> mappedResult = _mapper.MapModuleScreenToModuleScreenContracts(result);
return mappedResult;
}
}
Test code:
[Test]
public void ItCalls_ModuleRepository_Get_Method()
{
List<SayusiAndo.DiLib.DomainModel.Administration.ModuleScreen> queryResult = new List<SayusiAndo.DiLib.DomainModel.Administration.ModuleScreen>()
{
new DomainModel.Administration.ModuleScreen()
{
Id = 100,
},
};
var moduleScreenMock = new Mock<IModuleScreen>();
moduleScreenMock.Setup(c => c.GetAll()).Returns(queryResult);
administrationRepoMock.SetupProperty(c => c.ModuleScreen, moduleScreenMock.Object);
var mapperMock = new Mock<IMapper.IMapper.IMapper>();
var dilibApplicationHerlperMock = new Mock<IDiLibApplicationHelper>();
IDigitalLibraryApplication app = new DigitalLibraryApplication( administrationRepoMock.Object, mapperMock.Object, dilibApplicationHerlperMock.Object );
app.GetModules();
//issue is here
administrationRepoMock.Object.ModuleScreen.GetAll() //???
}
Here is a refactoring of your test that passes when run. You can update the pass criteria to suit you definition of a successful test.
[Test]
public void ItCalls_ModuleRepository_Get_Method() {
// Arrange
List<ModuleScreen> queryResult = new List<ModuleScreen>()
{
new ModuleScreen()
{
Id = 100,
},
};
//Building mapped result from query to compare results later
List<ModuleScreenContract> expectedMappedResult = queryResult
.Select(m => new ModuleScreenContract { Id = m.Id })
.ToList();
var moduleScreenMock = new Mock<IModuleScreen>();
moduleScreenMock
.Setup(c => c.GetAll())
.Returns(queryResult)
.Verifiable();
var administrationRepoMock = new Mock<IAdministrationRepository>();
administrationRepoMock
.Setup(c => c.ModuleScreen)
.Returns(moduleScreenMock.Object)
.Verifiable();
var mapperMock = new Mock<IMapper>();
mapperMock.Setup(c => c.MapModuleScreenToModuleScreenContracts(queryResult))
.Returns(expectedMappedResult)
.Verifiable();
//NOTE: Not seeing this guy doing anything. What's its purpose
var dilibApplicationHerlperMock = new Mock<IDiLibApplicationHelper>();
IDigitalLibraryApplication app = new DigitalLibraryApplication(administrationRepoMock.Object, mapperMock.Object, dilibApplicationHerlperMock.Object);
//Act (Call the method under test)
var actualMappedResult = app.GetModuleScreens();
//Assert
//Verify that configured methods were actually called. If not, test will fail.
moduleScreenMock.Verify();
mapperMock.Verify();
administrationRepoMock.Verify();
//there should actually be a result.
Assert.IsNotNull(actualMappedResult);
//with items
CollectionAssert.AllItemsAreNotNull(actualMappedResult.ToList());
//There lengths should be equal
Assert.AreEqual(queryResult.Count, actualMappedResult.Count());
//And there should be a mapped object with the same id (Assumption)
var expected = queryResult.First().Id;
var actual = actualMappedResult.First().Id;
Assert.AreEqual(expected, actual);
}
I use Automapper version 4.2.0 and I all Action Methods I have setting Like this :
var attributeGroups = _attributeGroupService.AttributeGroupDropdown();
var mapconfiguration = new MapperConfiguration(cfg => cfg.CreateMap<AttributeGroup, AttributeGroupViewModel>());
var mapper = mapconfiguration.CreateMapper();
var result = mapper.Map(attributeGroups, new List<AttributeGroupViewModel>());
I dont repeat this codes in all actions , I want create a generic method and just pass data to it and map it ,to do this , I create a Method like this ?
public N mapping<T,K,M,N>(T resource, K destination,M model,N newModel)
{
var mapconfiguration = new MapperConfiguration(cfg => cfg.CreateMap<T, K>());
var mapper = mapconfiguration.CreateMapper();
var result = mapper.Map<M,N>(model, newModel);
return result;
}
and call it like this :
var ResultTest=mapping<AttributeGroup,AttributeGroupViewModel,attributeGroups,new List<AttributeGroupViewModel>()>();
but it doesnt complie .
how can I do it?
Short answer: you need to provide types in the generic parameter list and variables in the argument list of your function call. Also, you don't need the T resource, K destination arguments at all, since they are unused.
There is another problem with your approach, since using it is not really convenient. So I suggest that you provide two specialized methods with reduced complexity.
See the below complete example. It contains the method mapAnything<...>, which is a working equivalent to your mapping<...> method. The methods mapObject<...> and mapCollection<...> are the specialized methods I was talking about.
class TestResource
{
public int Testnumber { get; set; }
public string Testtext { get; set; }
}
class TestDestination
{
public string Testtext { get; set; }
}
class Program
{
// equivalent to what you tried to do - needs explicit generic parameters on call
static N mapAnything<T, K, M, N>(M model, N newModel)
{
var mapconfiguration = new MapperConfiguration(cfg => cfg.CreateMap<T, K>());
var mapper = mapconfiguration.CreateMapper();
var result = mapper.Map<M, N>(model, newModel);
return result;
}
// variant for object mapping, where generics can be implicitely inferred
static N mapObject<M, N>(M model, N newModel)
{
var mapconfiguration = new MapperConfiguration(cfg => cfg.CreateMap<M, N>());
var mapper = mapconfiguration.CreateMapper();
var result = mapper.Map<M, N>(model, newModel);
return result;
}
// variant for lists, where generics can be implicitely inferred
static ICollection<N> mapCollection<M, N>(IEnumerable<M> model, ICollection<N> newModel)
{
var mapconfiguration = new MapperConfiguration(cfg => cfg.CreateMap<M, N>());
var mapper = mapconfiguration.CreateMapper();
var result = mapper.Map<IEnumerable<M>, ICollection<N>>(model, newModel);
return result;
}
static void Main(string[] args)
{
var res1 = new TestResource() { Testnumber = 1, Testtext = "a" };
var res2 = new List<TestResource>();
for (int i = 0; i < 10; i++)
{
res2.Add(new TestResource() { Testnumber = i, Testtext = "test: " + i });
}
var mapped1 = mapObject(res1, new TestDestination());
var mapped2 = mapCollection(res2, new HashSet<TestDestination>());
var mapped3 = mapAnything<TestResource, TestDestination, TestResource, TestDestination>(res1, new TestDestination());
var mapped4 = mapAnything<TestResource, TestDestination, IEnumerable<TestResource>, List<TestDestination>>(res2, new List<TestDestination>());
}
}
I was trying to unit test my repository. I need to mock the Where Clause with some values.
I have tried the following
var parentClass = new ParentClass {id = 1};
var subClass = new SubClass
{
id=2,
ParentClassId = parentClass.id,
ParentClass = parentClass
}
var subSubClass = new SubSubClass
{
id =3,
SubClassId = subClass.id,
SubClass = subClass
}
var dbContext = MockRepository.GenerateMock<IDbContext>();
var subClassContext = MockRepository.GenerateMock<IDbSet<SubClass>>();
var subSubClassContext = MockRepository.GenerateMock<IDbSet<SubSubClass>>();
subClassContext.Stub(x => x.GetEnumerator())
.Return(null)
.WhenCalled(c => c.ReturnValue = new List<SubClass> { subClass }.GetEnumerator());
subSubClassContext.Stub(x => x.GetEnumerator())
.Return(null)
.WhenCalled(c => c.ReturnValue = new List<SubSubClass> { subSubClass }.GetEnumerator());
I am testing the following code
var existingSubSubClasses = context.SubSubClass.Where(cba => cba.SubClass.ParentClassId == parentClassId).ToList();
I am getting ArgumentNullReferenceException
What am I doing wrong?
You should also stub context.SubSubClass property -- seems like it returns null by default.
I have a service layer between my app and the mongo database.
I'm trying to build a unit test using moq
I'm quite new to moq so I started with what I thought would be a trivial test.
Code to test:
public List<BsonDocument> GetAllSettings()
{
var collection = MongoDatabase.GetCollection<BsonDocument>("Settings");
var query = from e in collection.AsQueryable()
select e;
var settings = query.ToList();
return settings;
}
Where:
Settings is a Collection
MongoDatabase is a MongoDBDriver.MongoDatabase
I've tried this as my test:
[Test()]
public void GetAllSettingsTest()
{
//Arrange
BsonDocument doc01 = new BsonDocument();
BsonDocument doc02 = new BsonDocument();
var mongoDatabase = new Mock<MongoDatabase>();
var collection = new Mock<MongoCollection<BsonDocument>>();
mongoDatabase.Setup(f => f.GetCollection(MongoCollection.Settings)).Returns(collection.Object);
collection.Object.Insert(doc01);
collection.Object.Insert(doc02);
ILogger logger = new Logger();
DatabaseClient.DatabaseClient target = new DatabaseClient.DatabaseClient(logger);
target.MongoDatabase = mongoDatabase.Object;
MongoCursor<BsonDocument> cursor = collection.Object.FindAllAs<BsonDocument>();
List<BsonDocument> expected = cursor.ToList();
List<BsonDocument> actual;
//Act
actual = target.GetAllSettings();
//Assert
Assert.AreEqual(expected, actual);
}
I'm getting an error of "Could not find a parameterless constructor" at:
mongoDatabase.Setup(f => f.GetCollection(MongoCollections.Settings)).Returns(collection.Object);
The error refers to the MongoCollection object. I didn't think it had a constructor.
What can I do to get my test to run?
this question is most probably related to: How do I mock MongoDB objects to test my data models?
Anyway, here is minimal Moq configuration required to mock
var message = string.Empty;
var serverSettings = new MongoServerSettings()
{
GuidRepresentation = MongoDB.Bson.GuidRepresentation.Standard,
ReadEncoding = new UTF8Encoding(),
ReadPreference = new ReadPreference(),
WriteConcern = new WriteConcern(),
WriteEncoding = new UTF8Encoding()
};
var server = new Mock<MongoServer>(serverSettings);
server.Setup(s => s.Settings).Returns(serverSettings);
server.Setup(s => s.IsDatabaseNameValid(It.IsAny<string>(), out message)).Returns(true);
var databaseSettings = new MongoDatabaseSettings()
{
GuidRepresentation = MongoDB.Bson.GuidRepresentation.Standard,
ReadEncoding = new UTF8Encoding(),
ReadPreference = new ReadPreference(),
WriteConcern = new WriteConcern(),
WriteEncoding = new UTF8Encoding()
};
var database = new Mock<MongoDatabase>(server.Object, "test", databaseSettings);
database.Setup(db => db.Settings).Returns(databaseSettings);
database.Setup(db => db.IsCollectionNameValid(It.IsAny<string>(), out message)).Returns(true);
var mockedCollection = collection.Object;
Anyway, as I mentioned in linked question, this might not be useful when any of inner-workings of MongoDriver change.
I'm not familiar with the MongoDbDriver.MongoDatabase, but if it works like I think it does, then you can't mock it directly. You need to abstract the MongoDB access code, and mock that. That would be an actual unit test. e.g.
public interface IMongoDBRepository
{
Collection<T> GetCollection<T>(string name) where T BsonDocument;
}
public class MongoDbRepository : IMongoDBRepository
{
public Collection<T> GetCollection<T>(string name)
where T : BsonDocument
{
return MongoDatabase.GetCollection<BsonDocument>(name);
}
}
Now, in your code, you inject an IMongoDBRepository (using whatever DI method you like) and your code would looks something like this:
private IMongoDBRepository _mongoDBRepository; //this gets injected
public List<BsonDocument> GetAllSettings()
{
var collection = _mongoDBRepository.GetCollection<BsonDocument>("Settings");
var query = from e in collection.AsQueryable()
select e;
var settings = query.ToList();
return settings;
}
And finally your unit test:
[Test()]
public void GetAllSettingsTest()
{
//Arrange
BsonDocument doc01 = new BsonDocument();
BsonDocument doc02 = new BsonDocument();
var mongoDatabase = new Mock<IMongoDBRepository>();
var collection = new Mock<MongoCollection<BsonDocument>>();
mongoDatabase.Setup(f => f.GetCollection(MongoCollection.Settings)).Returns(collection.Object);
collection.Object.Insert(doc01);
collection.Object.Insert(doc02);
//rest of test
}