-1

I am learning unittesting and I am using Nsubstitute as a choice of mocking framework. so I have this small imaginary app I am writing for a guest inn. I am trying to write a test to ensure that once a reservation is made on a friday, room prices should be discounted by 20%. But I don't understand how this Nsubstitute works (mind you still learning) so I thought using the when() do() should help me test the scenario I aim. can someone help me understand the when() do() methods of the Nsubstitute framework? How do I complete this test? Hope y'all can help!!

public void DiscountRoomPricesAtWeekends(){
    //Arrange
    //prices at weekends should be discounted by 20%
    //get price of a particular room and discount price
    var roomprice = new Rooms() { Price = 100M };
    //if day to check in is a friday or saturday its 20% off
    var reserve = Substitute.For<Reservation>();
    //Make sure that the date of reservation is always friday.            
    //reserve.ReservationDate.DayOfWeek.Returns(DayOfWeek.Friday);
    //(double) roomprice.Price * 0.20) 
    var percentage = (double) roomprice.Price * 0.20;
    reserve.When(r => r.ReservationDate.DayOfWeek.Returns(DayOfWeek.Friday))
           .Do(p => );
    //Act 
    //Assert
    //Assert.AreEqual(percentage, 20);
    Assert.AreEqual();}

Thanks everyone

Joseph Izang
  • 766
  • 1
  • 13
  • 43
  • Well whoever it was that gave a -1 just doesn't get it. Everyone starts from somewhere when you are learning a new way of development. Anyways, I realized that for the scenario I spoke of, I had to run my test a different way. The point I was trying to make with my question is that the examples in Nsubstitute's docs is not really insightful in some areas. But anyways the answer below helped with some light and reminded me about all the theory I had read about unit testing. Cheers and God bless – Joseph Izang Mar 10 '15 at 02:17

1 Answers1

1

I can't tell from your example exactly what you are trying to test. If you post the code you are trying to test I may be able to assist you in coming up with a way to test it.

In terms of learning unit testing in general and mocking libraries in particular, I recommend testing without mocking libraries first. Start writing a test for the class/set of classes you are interested in. Along the way you may find bits that are difficult to reliably test (they rely on the current date/time, a database, random numbers, a web request etc). One way of dealing with this is to isolate these tricky bits from the rest of the code being tested.

Some examples of how this can be done are:

  • passing in the changeable values into the code being tested. For example, rather than calling DateTime.Now from within GetRoomPrice(), change the code to take the date the enquiry is for: GetRoomPrice(DateTime bookingDate).
  • push the logic that is hard to test behind an interface, then use a fake implementation of that interface for your tests. For example, if GetAvailableRooms() needs to access the database, you could create a RoomLookup class/interface that is passed in to your class being tested. For the test, pass in a fake version that returns a hard-coded value for GetAvailableRooms().

In my case, once I'd been doing the latter for a while, it became tedious to manually implement these fake classes. That then seemed a good time to switch from manually implementing these fake classes to a mocking library (Moq, FakeItEasy, NSubstitute, etc...) that will automate the process. Behind the scenes the mocking libraries are doing similar things to what we were previously doing manually. In my experience this approach makes mocking libraries much easier to understand.

That said, you may prefer the "passing in values" approach, in which case you might get away with not requiring mocking libraries at all! (or needing them so infrequently it is easier to code your fake classes by hand.)

Hope this helps.

David Tchepak
  • 9,826
  • 2
  • 56
  • 68