moq

Parameter count mismatch in a Mocked method call

断了今生、忘了曾经 提交于 2019-12-10 18:15:54
问题 I have this method: public void Valida(Validazione.IValidator<MyType> validator) { // do something... Validazione.IMapper<MyType> mapper = new MyTypeMapper(); ValidationResult result = validator.Validate(myTypeObj, mapper, new ValidationConfiguration()); // ...continue doing something else } that I want to unit test, so I would mock (using Moq framework) validator to steer the result of Validate method, so I wrote this unit test: [TestMethod] public void Long_test_name_as_best_practice() { //

Mocking an interface which is { get; } only (Moq)

泪湿孤枕 提交于 2019-12-10 17:46:09
问题 I have an IUnitOfWork interface which contains mappings to all of our repositories, like so: public interface IUnitOfWork : IDisposable { IRepository<Client> ClientsRepo { get; } IRepository<ConfigValue> ConfigValuesRepo { get; } IRepository<TestRun> TestRunsRepo { get; } //Etc... } Our IRepository class looks like this: public interface IRepository<T> { T getByID(int id); void Add(T Item); void Delete(T Item); void Attach(T Item); void Update(T Item); int Count(); } My issue is that I'm

Typemock vs Moq [closed]

泪湿孤枕 提交于 2019-12-10 17:41:41
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 7 years ago . I have heard/read a little about both of these frameworks and have decided I want to use of of the two. Which is better? Why? Thanks

Unit Testing a Custom Validation Filter

 ̄綄美尐妖づ 提交于 2019-12-10 17:39:14
问题 I have the following attribute: public class ValidateModelAttribute : ActionFilterAttribute { public override void OnActionExecuting(HttpActionContext actionContext) { if (actionContext.ModelState.IsValid == false) { actionContext.Response = actionContext.Request.CreateErrorResponse( HttpStatusCode.BadRequest, actionContext.ModelState); } } } I have this generic extension method for Determining whether the attribute is applied to a method or not public static bool ActionHasFilter(this

How to mock An Abstract Base Class

帅比萌擦擦* 提交于 2019-12-10 17:26:31
问题 I have a base class called "Question" and several child classes such as "TrueFalse", "MultipleChoice", "MatchPairs" etc... The base class has methods with logic that all of the child classes use, such as sending off scores and raising events. I have set my unit tests up for the child classes but I am not sure how I can setup unit tests for the methods in the base class. I did some searching and I understand I need to create a Mock of the class but I am not sure how to do this as I have only

How to mock UserManager<IdentityUser>

霸气de小男生 提交于 2019-12-10 17:13:04
问题 I am trying to work out how to add unit testing to my project. I thought it was best to start with a blank project and work it out from the start rather than adding it to my main project. Once I understand the process i figure i can start refactoring my project for adding testing. web application So i created a web application and added default user identity to it. This gave me a start up looking like this public void ConfigureServices(IServiceCollection services) { services.Configure

DbSet Find with Moq (EntityFramework.Testing.Moq)

限于喜欢 提交于 2019-12-10 16:47:43
问题 I've been using the Moq objects along with EntityFramework.Testing.Moq extension and I recently hit a bump trying to do a Find. What I had thought was right, that simply doing a setup like this (from the documentation): // Create some test data var data = new List<Blog> { new Blog{ Id = 1, Name = "BBB" }, new Blog{ Id = 2, Name = "CCC" }, new Blog{ Id = 3, Name = "AAA" } }; // Create a mock set and context var set = new Mock<DbSet<Blog>>() .SetupData(data); var context = new Mock

How do I get AutoFixture AutoMoq to return results from injected services in an instantiated object?

痞子三分冷 提交于 2019-12-10 15:49:38
问题 I am trying to test a service class that consumers a repository service. I have customizations set up that I believe should work with my repository service, but instead return default Anonymous results. If you look at the code sample below, I'm trying to get the "Foo" objects that I registered in the Customization Class back when I call the svc.GetFoos method, instead I get nothing: void Main() { var fixture = new Fixture().Customize( new CompositeCustomization( new Customization(), new

All invocation on the mock must have a corresponding setup

你。 提交于 2019-12-10 15:49:00
问题 I have some legacy code I want to unit test. I created a first moq test but I am getting the following exception: Moq.MockException:IConnection.SendRequest(ADF.Messaging.Contract.ConfigServer.GetDataVersionRequest) invocation failed with mock behavior Strict. All invocations on the mock must have a corresponding setup. Important pieces of code: Property on class: Public Property Connection() As IConnection Get Return _connection End Get Set(ByVal value As IConnection) _connection = value End

Moq and Code Contracts

放肆的年华 提交于 2019-12-10 15:26:26
问题 When using class invariants, Code contracts seems to inject code everywhere. Stuff like this [ContractClassFor(typeof(IX))] interface IXContract { [ClassInvariant] void Invariant() { ... } } [ContractClass(typeof(IXContract))] interface IX { event EventHandler b; } var a = new Mock<IX>(); a.Raise(x => x.b += null); Fails with an error message Could not locate event for attach or detach method Void $InvariantMethod$(). Anyone know of a solution? 回答1: This unit test "passes" when run without