I am writing a unit tests using Xunit
and moq
. Within one function it calls Process.GetProcessesByName()
. I need this to return a process with a certain pid
which would exist when my program is actually running. However since in the test this process won't exist how do I mock away this call to return a fake process I want so I can continue to test the rest of the method?
Asked
Active
Viewed 1,528 times
0
2 Answers
1
You will need to wrap the call to Process.GetProcessesByName()
in an injectable class in order to mock it. There is no way for Moq
to replace behaviour of static methods.
There's a good answer on this topic here: How to mock static methods in c# using MOQ framework?

DaggeJ
- 2,094
- 1
- 10
- 22
0
What you might be trying is a system testing/Integration testing. Trying to get actual value during unit testing is not what Unit testing is meant to do. The scope of unit testing is to validate the functionality of the piece of code not on the data. If you are interested in testing the actual data, try other testing methods like automation testing.

Carbine
- 7,849
- 4
- 30
- 54