moq

How to Unit Test HtmlHelper similar to “using(Html.BeginForm()){ }”

泄露秘密 提交于 2019-12-12 09:43:39
问题 Can somebody please suggest how I could write a Unit Test with Moq for following HtmlHelper method? public static HtmlTagBase GenerateTag<T>(this HtmlHelper htmlHelper , object elementData , object attributes) where T : HtmlTagBase { return (T)Activator.CreateInstance(typeof(T) , htmlHelper.ViewContext , elementData , attributes); } which you would use as follows (please note the using statement - this is causing me confusion): <%--Model is a type of ShareClass--%> <% using (Html.GenerateTag

Accessing the original arguments of Expect() when assembling the value in Returns()

北城余情 提交于 2019-12-12 09:29:04
问题 Is it possible to get access to the parameter used to make a call to a mocked expectation when assembling the Returns object? Here is a stub for the objects involved and, given that, I am trying to mock a Collection: Class CollectionValue { public Id { get; set; } } Class Collection { private List<CollectionValue> AllValues { get; set; } public List<CollectionValue> GetById(List<int> ids) { return AllValues.Where(v => ids.Contains(v.Id)); } } Given a test list of CollectionValues that will be

System.NotSupportedException: Invalid setup on a non-virtual (overridable in VB) member

隐身守侯 提交于 2019-12-12 09:10:01
问题 I am getting a NotSupportedException error message on my Unit Test using Moq System.NotSupportedException: Invalid setup on a non-virtual (overridable in VB) member Unit Test Code: [TestMethod] public void TestEmailNotSentOut() { // ... var dataAccess = new Mock<TjiContext>(); var mockSetStock = new Mock<DbSet<Stock>>(); mockSetStock.As<IQueryable<Stock>>().Setup(m => m.Provider).Returns(stockList.Provider); mockSetStock.As<IQueryable<Stock>>().Setup(m => m.Expression).Returns(stockList

How to mock to execute oracle query to the in memory data

[亡魂溺海] 提交于 2019-12-12 08:55:55
问题 I am trying to write unit test to execute an oracle query to filter the in memory list of object. How do i mock so that the filter condition will be applied to my in memory list of object rather than the actual database? I could achieve this with the Entity Framework where I can mock the context and return in memory data but don't know how to achieve the same using OracleCommand.ExecuteReader . using (var connection = new OracleConnection(connectionString)) { connection.Open(); var cmd = new

Can you mock an object that implements an interface AND an abstract class?

微笑、不失礼 提交于 2019-12-12 08:29:57
问题 Is it possible to use Moq to mock an object that implements an interface and abstract class? I.e.: public class MyClass: SomeAbstractClass, IMyClass Can you mock this? 回答1: You can mock any interface, and any abstract or virtual members. That's basically it. This means that the following are absolutely possible: var imock = new Mock<IMyClass>(); var aMock = new Mock<SomeAbstractClass>(); If the members inherited from SomeAbstractClass aren't sealed, you can also mock MyClass: var mcMock = new

MOQ - how to manually create a backing property using SetupGet/SetupSet?

ぃ、小莉子 提交于 2019-12-12 07:56:04
问题 I know we can call SetupAllProperties() to automatically create backing properties. But this is too restrictive, because it doesn't allow me to execute additional code in the getter/setters. For example, I'd like to create a moq'd setter that invokes some other method/event/logic. The Following code sample reproduces the issue public interface IA { int B { get; set; } }; class Test { [Test] public void BackingPropertyTest() { int b = 1; var mockA = new Mock<IA>(); //mockA.SetupAllProperties()

How to use Unity.RegisterType with Moq?

痞子三分冷 提交于 2019-12-12 07:44:52
问题 I have a running code with unity. Now I want to use Moq to do my unit testing for ASP-MVC. In the global.asax's code, I have the following: IUnityContainer container = new UnityContainer(); container.RegisterType<IFoo, Foo>(new InjectionConstructor("xxx")); Now I wrote testcode with Moq: IUnityContainer container = new UnityContainer(); var mockFoo = new Mock<IFoo>(); container.RegisterType<IFoo, mockFoo) >(new InjectionConstructor("xxx")); but this don't work. Error: The type 'Moq.Mock'

AutoFixture as an Automocking container vs Automocking differences?

人走茶凉 提交于 2019-12-12 07:12:18
问题 I started to use moq but from my understanding I always have to mock up all the methods that could be called even if I really do not care about them. Sometimes it takes so long to mockup stuff you forget what you want to do. So I been looking at auto mocking but I am not sure what one I should use. AutoFixture as an auto-mocking container Automocking I don't get how to use the first one at all. I sort of get the second one but never really tried it. I am not sure if one is better than the

How to mock a melthod with out paramter in Moq AND fill the out

徘徊边缘 提交于 2019-12-12 04:57:22
问题 I have see several questions here in Stackoverflow about out parameters in MOQ, my question is how fill this parameter: Lets to code: [HttpPost] public HttpResponseMessage Send(SmsMoRequest sms) { if (sms == null) return Request.CreateResponse(HttpStatusCode.BadRequest); SmsMoResponse response; _messageService.Process(sms, out response); return Request.CreateResponse(HttpStatusCode.Created, response.ToString()); } I want to test this post: [Test] public void Should_Status_Be_Create_With_Valid

How to UnitTest a Function in a mocked method

一个人想着一个人 提交于 2019-12-12 04:56:35
问题 How can I test the DeleteAppointmentById here? Func<IDataAdapterRW, IEnumerable<uint>> function = db => DeleteAppointmentById(db, appointmentId); return _dataContextProvider.GetContextRW().Run(function); _dataContextProvider is mocked with moq. If I run the test it never enters DeleteAppointmentById of course The method to test: public IEnumerable<uint> DeleteAppointment(uint appointmentId) { Func<IDataAdapterRW, IEnumerable<uint>> function = db => DeleteAppointmentById(db, appointmentId);