Unit test of the second method call - c#

I have a unit test where I am using Moq and Fluent Assertions:
[Fact]
public void GetSymbols_ShouldSetSucceedToTrue_WhenSecondAttemptSucceed()
{
string selectedFileName = "testFileName.txt";
string[] expectedResult = new string[] { "testSymbol1", "testSymbol2" };
Mock<IOpenFileDialogService> mockFileDialogService = new Mock<IOpenFileDialogService>();
mockFileDialogService.SetupSequence(m => m.ShowDialog()).Returns(false).Returns(true);
mockFileDialogService.Setup(m => m.FileName).Returns(selectedFileName);
Mock<IFileService> mockFileService = new Mock<IFileService>();
mockFileService.Setup(m => m.ReadAllLines(selectedFileName)).Returns(expectedResult);
SymbolsProviderFromFile spff = new SymbolsProviderFromFile(mockFileDialogService.Object, mockFileService.Object);
// Act
spff.GetSymbols();
IEnumerable<string> result = spff.GetSymbols();
// Assert
using (new AssertionScope())
{
result.Should().Equal(expectedResult);
spff.Succeed.Should().BeTrue();
}
}
I would like to check the second call of my method.
Unfortunately when I debug this code, the spff.GetSymbols() method is only called once and it is called on the result.Should().Equals(expectedResult) line when the result is checked. There is some kind of a lazy loading here - the method is only called when the result is needed.
Why is it not called immediately in the spff.GetSymbols() line? How can I change this behavior and how can I call testing method twice in unit test?

Problem has been resolved thanks to #Dennis Doomen.
An issue about not executing method immediately was in the implementation of the spff.GetSymbols() method which uses yield return so it wasn't really related to unit testing.

Related

FakeItEasy not verifying call for Full Framework SignalR test

I have a simple hub that I am trying to write a test for with FakeItEasy and the verification of calling the client is not passing. I have the example working in a separate project that uses MOQ and XUnit.
public interface IScheduleHubClientContract
{
void UpdateToCheckedIn(string id);
}
public void UpdateToCheckedIn_Should_Broadcast_Id()
{
var hub = new ScheduleHub();
var clients = A.Fake<IHubCallerConnectionContext<dynamic>>();
var all = A.Fake<IScheduleHubClientContract>();
var id= "123456789";
hub.Clients = clients;
A.CallTo(() => all.UpdateToCheckedIn(A<string>.Ignored)).MustHaveHappened();
A.CallTo(() => clients.All).Returns(all);
hub.UpdateToCheckedIn(id);
}
I'm using Fixie as the Unit Test Framework and it reports:
FakeItEasy.ExpectationException:
Expected to find it once or more but no calls were made to the fake object.
The sample below works in XUnit & MOQ:
public interface IScheduleClientContract
{
void UpdateToCheckedIn(string id);
}
[Fact]
public void UpdateToCheckedIn_Should_Broadcast_Id()
{
var hub = new ScheduleHub();
var clients = new Mock<IHubCallerConnectionContext<dynamic>>();
var all = new Mock<IScheduleClientContract>();
hub.Clients = clients.Object;
all.Setup(m=>m.UpdateToCheckedIn(It.IsAny<string>())).Verifiable();
clients.Setup(m => m.All).Returns(all.Object);
hub.UpdateToCheckedIn("id");
all.VerifyAll();
}
I'm not sure what I've missed in the conversion?
You're doing some steps in a weird (it looks to me, without seeing the innards of your classes) order, and I believe that's the problem.
I think your key problem is that you're attempting to verify that all.UpdateToCheckedIn must have happened before even calling hub.UpdateToCheckedIn. (I don't know for sure that hub.UpdateToCheckedIn calls all.UpdateToCheckedIn, but it sounds reasonable.
There's another problem, where you configure clients.Setup to return all.Object, which happens after you assert the call to all.UpdateToCheckedIn. I'm not sure whether that's necessary or not, but thought I'd mention it.
The usual ordering is
arrange the fakes (and whatever else you need)
act, but exercising the system under test (hub)
assert that expected actions were taken on the fakes (or whatever other conditions you deem necessary for success)
I would have expected to see something more like
// Arrange the fakes
var all = A.Fake<IScheduleHubClientContract>();
var clients = A.Fake<IHubCallerConnectionContext<dynamic>>();
A.CallTo(() => clients.All).Returns(all); // if All has a getter, this could be clients.All = all
// … and arrange the system under test
var hub = new ScheduleHub();
hub.Clients = clients;
// Act, by exercising the system under test
var id = "123456789";
hub.UpdateToCheckedIn(id);
// Assert - verify that the expected calls were made to the Fakes
A.CallTo(() => all.UpdateToCheckedIn(A<string>.Ignored)).MustHaveHappened();

RhinoMocks VerifyAllExpectations fails for multiple Expects on stubbed method

Dear RhinoMocks users out there. I am a newbie to RhinoMocks and have a problem wrapping my head around something. I need to test two methods in a class, on of them calls the other multiple times. I already tested the one called multiple times separately, the setup is somewhat complex so the idea for testing the other method was to stub the method that has been tested already. Here is a minimal example:
public class TestedClass
{
public virtual void DoSthOnce(List<int> listParam)
{
foreach (var param in listParam)
DoSthMultipleTimes(param);
}
public virtual void DoSthMultipleTimes(int intParam)
{
Console.WriteLine("param: " + intParam);
}
}
I.e. DoSthMultipleTimes() is already tested. The following test code works and verifies that the DoSthMultipletimes()-Method was called for each element of the list that was provided as parameter to DoSthOnce().
var paramList = Enumerable.Range(1, 10).ToList();
var mock = MockRepository.GeneratePartialMock<TestedClass>();
mock.Stub(m => m.DoSthMultipleTimes(Arg<int>.Is.Anything))
.WhenCalled(mi =>
{
// Only for debug; this method is empty in the actual test code.
Console.WriteLine("Stub called with " + mi.Arguments[0]);
});
mock.DoSthOnce(paramList);
// This will not throw an exception
foreach (var param in paramList)
mock.AssertWasCalled(m => m.DoSthMultipleTimes(param));
The output is as expected:
Stub called with 1
Stub called with 2
Stub called with 3
Stub called with 4
Stub called with 5
Stub called with 6
Stub called with 7
Stub called with 8
Stub called with 9
Stub called with 10
However, the following fails throwing an exception although it should be the same thing as above, at least from my understanding:
var paramList = Enumerable.Range(1, 10).ToList();
var mock = MockRepository.GeneratePartialMock<TestedClass>();
mock.Stub(/*same as above*/).WhenCalled(/*same as above*/);
foreach (var param in paramList)
mock.Expect(m => m.DoSthMultipleTimes(param));
mock.DoSthOnce(paramList);
mock.VerifyAllExpectations();
The console output is identical, but an ExpectationViolationException is thrown by VerifyAllExpectations(). Additional information is:
TestedClass.DoSthMultipleTimes(1); Expected #1, Actual #0.
TestedClass.DoSthMultipleTimes(2); Expected #1, Actual #0.
...
TestedClass.DoSthMultipleTimes(10); Expected #1, Actual #0.
The parameters are correct, but what exactly is the problem here?
I'm guessing because of your stub itself.
This piece of code would register the various expectations that you want:
foreach (var param in paramList)
mock.Expect(m => m.DoSthMultipleTimes(param));
This would set the Expectation that DoSthMultipleTimes(1), DoSthMultipleTimes(2), etc are being called.
However your Mock.Stub code would override / stubbing the implementation itself. This resulted that Rhino would execute this override/method (and not calling the original DoSthMultipleTimes method of the original class). As such, when VerifyAllExpectation being called, none of the method in original class was called.
You could verify the above by removing the following:
mock.Stub(m => m.DoSthMultipleTimes(Arg<int>.Is.Anything))
.WhenCalled(mi =>
{
// Only for debug; this method is empty in the actual test code.
Console.WriteLine("Stub called with " + mi.Arguments[0]);
});
Since you are using PartialMock, if you are not implementing the stub, it would call the original method. And supposedly, it would now successfully pass all the expectation.
(NB: beware when using PartialMock as it would run the original method if not being stubbed (ie: database call, etc)).

C# mocking Mock<StreamWriter>

I am new to C# mocking, I am trying to read some code and I got one of the tests failing, can you please explain to me what the below source code is trying to test and when will it fail?
Mock<StreamWriter> _streamWriterMock;
string[] expectedLines;
.
.
.
foreach (var line in expectedLines)
{
_streamWriterMock.Verify(a => a.Write(line), Times.Exactly(1));
}
Verify
You might want to check that the method under test was called, or even how many times that method was called
Just to reproduce the issue try this code
class Program
{
static void Main(string[] args)
{
var _streamWriterMock = new Mock<StreamWriter>("output.txt");
string[] expectedLines= new []{"test","test"};
foreach (var expectedLine in expectedLines)
{
_streamWriterMock.Object.Write(expectedLine);
}
foreach (var line in expectedLines)
{
_streamWriterMock.Verify(a=>a.Write(line),Times.Exactly(1));
}
}
}
In fact, if you try to mock your code with the array {"test","test"} you will get an exception
Expected invocation on the mock exactly 1 times, but was 2 times: a => a.Write("test")
But if your array is something like the following
string[] expectedLines= new []{"test","test1"};
Your mock will be executed correctly
So your verify will check if your method is called exactly once for the same input.
I think that the code main goal is to omit that you write the same output twice.
The test in your example iterates through all of the strings in your expectedLines array and checks that _streamWriterMock.Write(string value) is called on each of them exactly once. It will fail if Write is not called or is called more than once on any of the strings.
Update
Generally mocked methods must be virtual and depending on your mocking framework, the mock's method may need to be setup before being called so it may not be a valid test at all since StreamWriter is a concrete class and Write is not a virtual method.
Given the mocked StreamWriter
At best the foreach loop being used to verify with the mock that the each string in the expected lines array was called on the mocked stream writer's Write method exactly once during the exercising of the subject under test.
The test will fail if any of the expected lines are written more than once.
Review Moq: Quickstart - Verification
Take the following class as an example of a possible subject that depends on a StreamWriter
public class SubjectUnderTest {
private StringWriter stringWriter;
public SubjectUnderTest(StringWriter stringWriter) {
this.stringWriter = stringWriter;
}
public void WriteLines(string[] lines) {
foreach (var line in lines) {
this.stringWriter.Write(line);
}
}
}
The dependency would be mocked when testing and the functionality of the method under test can be verified in isolation.
For example.
[TestMethod]
public void TestMethod1() {
//Arrange
var _streamWriterMock = new Mock<StringWriter>();
string[] expectedLines = new[] { "line1", "line2" };
var subject = new SubjectUnderTest(_streamWriterMock.Object);
//Act
subject.WriteLines(expectedLines);
//Assert
foreach (var line in expectedLines) {
_streamWriterMock.Verify(a => a.Write(line), Times.Exactly(1));
}
}
If however, expectedLines had duplicates like { "lineN", "lineN" } Then the above test would fail as the verification is expecting the Write method to be called exactly one time with a given string value.

Ensure that the method under test has thrown the expected exception, NOT any other part of the test case set up

I am using Visual Studio Unit Test Cases.
I have written the Unit test case where Argument Exception is expected from the method under test MethodUnderTest. Suppose if any other part of the test case (Setup part) throws the expected exception ArgumentException, Then I want to enforce that my test case should fail. It should pass only in case Setup is correct and instance.MethodUnderTest(); line of code throws ArgumentException.
I can achieve using try catch, but I want to know is there any better approach to achieve this.
[ExpectedException(typeof(ArgumentException))]
public void TestCaseMethod()
{
// Set up
Mock<ITestClass> testM = new Mock<ITestClass>();
AnimalClass instance = new AnimalClass(testM.Object);
// call the method under test
instance.MethodUnderTest();
}
If you use a more advanced unit testing framework, like NUnit. you can do things like:
// Act
var result = Assert.Throws<Exception>(() => instance.MethodUnderTest));
// Assert
Assert.IsInstanceOf<ArgumentException>(result);
I don't know of any built in way, but you could wrap up the method in an assert exception
private void AssertException<T>(Action method)
where T : Exception
{
try
{
method();
Assert.Fail();
}
catch (T e)
{
Assert.IsTrue(true);
}
}
Then call with
[TestMethod]
public void TestCaseMethod()
{
// Set up
Mock<ITestClass> testM = new Mock<ITestClass>();
AnimalClass instance = new AnimalClass(testM.Object);
// call the method under test
AssertException<ArgumentException>(instance.MethodUnderTest)
}
Or, if your method takes in parameters or returns values
AssertException<MyException>(() => instance.ParameterisedFunction(a, b));

rhinomocks setting expectation, unit test always passes

I'm trying to become more familiar with the Rhinomocks framework, and I'm trying to understand the Expect methods of rhinomocks.
Here's a unit test I have written:
[TestMethod]
public void Create_ValidModelData_CreatesNewEventObjectWithGivenSlugId()
{
//Arrange
var eventList = new List<Event>() { new Event() { Slug = "test-user" } };
_stubbedEventRepository.Stub(x => x.GetEvents())
.Return(eventList);
_stubbedEventRepository
.Expect(x => x.SaveEvent(eventList.SingleOrDefault()))
.Repeat
.Once();
var controller = new EventController(_stubbedEventRepository);
EventViewModel model = new EventViewModel();
//Act
//controller.Create(model); COMMENTED OUT
//Assert
_stubbedEventRepository.VerifyAllExpectations();
}
I thought I understood this code to only pass if the SaveEvent(...) method get's called exactly once. However, with controller.Create(model) commented out, the test still passes. Inside controller.Create(model) is where the SaveEvent() method gets called.
I tried the following:
_stubbedEventRepository
.Expect(x => x.SaveEvent(eventList.SingleOrDefault()));
But it still passes every time, so what am I doing incorrectly stack overflow? The sources I have looked at online haven't been able to help me. Why is VerifyAllExpectations() yielding a successful unit test?
Thank you!
Here's the body of the controller constructor:
public EventController(IEventRepository eventRepository)
{
_eventRepository = eventRepository;
}
edit:
// member variables
private IEventRepository _stubbedEventRepository;
[TestInitialize]
public void SetupTests()
{
_stubbedEventRepository = MockRepository.GenerateStub<IEventRepository>();
}
If you want to verify the behavior of the code under test, you will use a mock with the appropriate expectation, and verify that. If you want just to pass a value that may need to act in a certain way, but isn't the focus of this test, you will use a stub.

Categories

Resources