autofixture

How can I tell AutoFixture to always create TDerived when it instantiates a TBase?

时间秒杀一切 提交于 2019-12-10 12:34:40
问题 I have a deeply-nested object model, where some classes might look a bit like this: class TBase { ... } class TDerived : TBase { ... } class Container { ICollection<TBase> instances; ... } class TopLevel { Container container1; Container container2; ... } I'd like to create my top-level object as a test fixture, but I want all the TBase instances (such as in the instances collection above) to be instances of TDerived rather than TBase . I thought I could do this quite simply using something

How do I apply AutoFixture customizations to anything inheriting from a base class?

大憨熊 提交于 2019-12-10 04:07:59
问题 In an attempt to DRY up my unit tests, I'm trying to use AutoFixture as an IoC container to instantiate my system under test (SUT), which in this particular case are ASP.NET MVC Controller s. Therefore, I want to customize AutoFixture to create controllers without auto-properties. I tried adding a customization for ControllerBase , but it doesn't seem to work for subclasses of ControllerBase . fixture.Customize<ControllerBase>(c => c.OmitAutoProperties()); Here's an example of the test I want

How to get AutoFixture create an integer that is >0, and not another number?

寵の児 提交于 2019-12-10 01:45:45
问题 I want AutoFixture to generate two integers, and for the second one, I don't want it to be 0, or the previous generated number. Is there a way to tell AutoFixture to honor that "requirement". Looking at RandomNumericSequenceGenerator , I looks like the lower limit is 1, so I might not have to specify the first requirement. Next, I was looking at the "seeding" option, but as indicated in this answer, it won't be used for a number, by default. Is there something I'm overlooking here? 回答1: Here

AutoFixture IEnumerable<T> behavior with CreateMany()

六月ゝ 毕业季﹏ 提交于 2019-12-09 02:54:46
问题 When looking at the post here, it looks like I should be able to create several objects using CreateMany() , iterate over them using foreach , and then return them as an array. What I'm seeing is that each iteration seems to create new objects each time. Is this expected behavior? Entity to create: public class TestEntity { public int Id { get; private set; } public string SomeString { get; set; } public void SetId(int value) { this.Id = value; } } Sample Program.cs: private static int id;

Creating a domain model without circular references in Entity Framework

橙三吉。 提交于 2019-12-09 02:25:00
问题 I have found a solution that works (using DTOs and AutoMapper), which is reproduced below, but I would prefer an answer that lists the different approaches to the problem with examples and this will be marked as the answer if received. In my entity model I have a navigation property that goes from a child entity to the parent entity. My project was working swimmingly. Then I began to use AutoFixture for unit testing, and testing failed, AutoFixture saying I had a circular reference. Now, I

How can I add common postprocessing applied after customization

情到浓时终转凉″ 提交于 2019-12-08 18:37:10
问题 I have defined ISpecimenBuilder for my models and use it like that: new Fixture().Customize(new ModelCustomization()); I want to use it in most of my tests concerning model. I also want to apply some form of post-processing in one of my test classes. Specifically I want to fill property CompanyHistory of all created Offers . It feels like it could be done like that: fixture.Build<Offer>() .With(o => o.CompanyHistory, _previouslyCreatedCompanyHistory) .Create(); But Build<T> disables all

AutoConfiguredMoqCustomization and unsettable properties

笑着哭i 提交于 2019-12-08 17:38:24
问题 How do I force AutoFixture, that has been configured with AutoConfiguredMoqCustomization, to automatically mock interfaces and its read-only properties? To make things clear, let's assume I have such an interface: public interface A { int Property {get;} } and such class: public class SomeClass { public SomeClass(A dependency) {} } What I want is to have dependency resolved to a mock that will return something in dependency.Property : var fixture = new Fixture().Customize(new

Autofixture and Moq v4

删除回忆录丶 提交于 2019-12-08 15:15:29
问题 I installed Autofixture and Moq using Nuget.So I have moq version 4. When running the following code var fixture = new Fixture().Customize(new AutoMoqCustomization()); fixture.CreateAnonymous<ISomething>(); the following error shows up System.IO.FileLoadException : Could not load file or assembly 'Moq, Version=3.1.416.3, Culture=neutral, PublicKeyToken=69f491c39445e920' I've also tried redirected it to the v4,but with no luck. <configuration> <runtime> <assemblyBinding> <dependentAssembly>

How to use AutoFixture in this case?

旧时模样 提交于 2019-12-07 22:53:54
问题 I have the following code: var boundArgument = new BoundArgumentOption { PatientId = patientId }; var mockRepositoryFactory = A.Fake<IRepositoryFactory>(); var sut = new HtmlOutputBuilder(mockRepositoryFactory); var patientRecord = // Some record; var mockRepository = A.Fake<IRepository>(); A.CallTo(() => mockRepository.Get(boundArgument)).Returns(patientRecord); A.CallTo(() => mockRepositoryFactory.Create(boundArgument)).Returns(mockRepository); string actualResult = sut.BuildReport

Unit Testing an Html Helper with AutoFixture

蓝咒 提交于 2019-12-07 22:32:47
问题 I'm attempting to Unit Test an Html Helper using AutoFixture. Below is my SUT public static MvcHtmlString SampleTable(this HtmlHelper helper, SampleModel model, IDictionary<string, object> htmlAttributes) { if (helper == null) { throw new ArgumentNullException("helper"); } if (model == null) { throw new ArgumentNullException("model"); } TagBuilder tagBuilder = new TagBuilder("table"); tagBuilder.MergeAttributes(htmlAttributes); tagBuilder.GenerateId(helper.ViewContext.HttpContext.Items[Keys