nmock

Mocking a private field

左心房为你撑大大i 提交于 2019-12-22 09:48:57
问题 I know a similar question has been asked but I have not found a clear solution. I'm trying to mock a private field from a large class. The private field gets instantiated in some earlier method and I'm trying to unit test a latter method which references the field. So I have an earlier method in my class: public bool validateAll(ref DataEntry[] oEntries, string sMediaPlanId, ITemplateGenerator oTempGen) { ... // private field that I am trying to mock this._sMediaPlanObjective = (MPWrapper

Test a public method which calls a private method using NUnit

元气小坏坏 提交于 2019-12-22 08:39:03
问题 I have a public method in a class that internally calls a particular private method within that class. It looks something like this : public class MyClass : IMyClassInterface { public List<int> MyMethod(int a, int b) { MyPrivateMethod(a, b, ref varList, ref someVal); } private void MyPrivateMethod(int a, int b, ref List<int> varList, ref double someval) { } } Now, I basically want to test this public method using NUnit. I am using NMock 2.0 for mocking. How do I do it? Since, it internally

When to Expect and When to Stub?

心不动则不痛 提交于 2019-12-18 12:23:48
问题 I use NMock2, and I've drafted the following NMock classes to represent some common mock framework concepts: Expect : this specifies what a mocked method should return and says that the call must occur or the test fails (when accompanied by a call to VerifyAllExpectationsHaveBeenMet() ). Stub : this specifies what a mocked method should return but cannot cause a test to fail. So which should I do when? 回答1: A lot of mocking frameworks are bringing the concepts of mocks & stubs closer & closer

NMock issue testing against WPF and Dispatcher

僤鯓⒐⒋嵵緔 提交于 2019-12-12 18:35:54
问题 Here's one for the threading junkies out there. I've got this method: public void RefreshMelts() { MeltsAvailable.Clear(); ThreadPool.QueueUserWorkItem(delegate { Dispatcher.BeginInvoke((ThreadStart)delegate { eventAggregator.GetEvent<BusyEvent>().Publish(true); eventAggregator.GetEvent<StatusMessageEvent>().Publish( new StatusMessage("Loading melts...", MessageSeverity.Low)); }); try { IList<MeltDto> meltDtos = meltingAppService.GetActiveMelts(); Dispatcher.Invoke((ThreadStart)delegate {

C# Mock a Class With an Internal Property Setter

人走茶凉 提交于 2019-12-08 04:00:49
问题 I am trying to build a unit test. The class Position is implemented in a third party library . But for my unit test I need the Size property to be set to a specific value. public class Position { private double _size; private double Size { get { return _size; } internal set { _size = value; } } } I read this post: How do you create a unit-testing stub for an interface containing a read-only member? but could not figure out how to make it work for me. This is the class under test (just a

Mocking a private field

倾然丶 夕夏残阳落幕 提交于 2019-12-05 19:03:38
I know a similar question has been asked but I have not found a clear solution. I'm trying to mock a private field from a large class. The private field gets instantiated in some earlier method and I'm trying to unit test a latter method which references the field. So I have an earlier method in my class: public bool validateAll(ref DataEntry[] oEntries, string sMediaPlanId, ITemplateGenerator oTempGen) { ... // private field that I am trying to mock this._sMediaPlanObjective = (MPWrapper.Instance).getMediaPlanObjective(sMediaPlanId); ... } And I'm trying to Unit test a method that references

Test a public method which calls a private method using NUnit

被刻印的时光 ゝ 提交于 2019-12-05 16:49:36
I have a public method in a class that internally calls a particular private method within that class. It looks something like this : public class MyClass : IMyClassInterface { public List<int> MyMethod(int a, int b) { MyPrivateMethod(a, b, ref varList, ref someVal); } private void MyPrivateMethod(int a, int b, ref List<int> varList, ref double someval) { } } Now, I basically want to test this public method using NUnit. I am using NMock 2.0 for mocking. How do I do it? Since, it internally calls this private method which I do not want to make public. Or is there a way to do it if I turn the

Is there a way to unit test an async method?

亡梦爱人 提交于 2019-12-04 15:42:07
问题 I am using Xunit and NMock on .NET platform. I am testing a presentation model where a method is asynchronous. The method creates an async task and executes it so the method returns immediately and the state I need to check aren't ready yet. I can set a flag upon finish without modifying the SUT but that would mean I would have to keep checking the flag in a while loop for example, with perhaps timeout. What are my options? 回答1: Does your object feature any sort of signal that the

Is there a way to unit test an async method?

百般思念 提交于 2019-12-03 09:46:26
I am using Xunit and NMock on .NET platform. I am testing a presentation model where a method is asynchronous. The method creates an async task and executes it so the method returns immediately and the state I need to check aren't ready yet. I can set a flag upon finish without modifying the SUT but that would mean I would have to keep checking the flag in a while loop for example, with perhaps timeout. What are my options? Does your object feature any sort of signal that the asynchronous method is finished, such as an event? If that is the case, you can use the following approach: [Test]

When to Expect and When to Stub?

微笑、不失礼 提交于 2019-11-25 15:18:25
I use NMock2, and I've drafted the following NMock classes to represent some common mock framework concepts: Expect : this specifies what a mocked method should return and says that the call must occur or the test fails (when accompanied by a call to VerifyAllExpectationsHaveBeenMet() ). Stub : this specifies what a mocked method should return but cannot cause a test to fail. So which should I do when? A lot of mocking frameworks are bringing the concepts of mocks & stubs closer & closer together to the point that they can be considered functionally almost the same. From a conceptual