What are the valid test cases for method calls within if-else condition

I have a discount service that gets called if certain conditions are met. I need to write test cases to check if the discount service is called. My doubt is checking if the discount service is NOT called for type 4 and 5 is a valid test case scenario.

public void ApplyDiscount(int typeId, double amount)
{

if(typeId == 1)
{
  //call discount service
}

else if(typeId == 2)
{
  //call discount service
}

else if(typeId == 4)
{
   //do other stuff
}
else if(typeId == 5)
{
   //do some other stuff
}

}

(I’m most familiar with Java, Junit, and mockito – so what I write below is based on my understanding of this applied to what I can find)

You are looking for a mocking tool such as Moq.

In this scenario what you would do is mock the object that contains the DiscountService.

At this point, you would do something like (I have no way of verifying if the following code is correct for the framework, but its in the right direction):

public void Test() {
    var mockService = new Mock<IDiscountService>();
    SomeClass.ApplyDiscount(1, 0.0);
    mockService.Verify(
       mockService = foo.ApplyDiscount(), Times.Once());
}

And this will verify that the discount service was called once (and only once) when the ApplyDiscount call was made with the given parameter.

I will caution you that the structure of this code that you have psuedocoded out seems suspect too. The int being passed in and what looks to be something that wants to be a switch statement smells like wishing to be polymorphism.

Consider that if the test is hard to write, sometimes it means that the design of the system is wrong and should be revisited. Well written code tends to be easy to test.

Still, you will want to read up on Moq and use it in the right places (like verifying that something was called the appropriate number of times without putting test code into your actual code – which smells even worse than switches).

At this point there are no valid test cases.

There’s not a lot to test here. The method returns void, so any test would be focused on any effect to an object or state that is internal to this method, or alters state the is global in scope. If cases 1 and 2 simply call a service then there is no change in state, so there is nothing to test.

Cases 4 and 5, don’t know what kind of stuff you are doing so at this point I would not write any tests for this.

Additionally, I would probably use a switch statement instead of a bunch of if/elses. Looks like case 1 and 2 are the same.

1

The most easy way to discover the test you nee its thinking in the list of features you want form the code under test, for example:

  • “when type is 1,2 call discount service”
  • “when type is 4 do some stuff”
  • “when type is 5 do some other stuff”

Perhaps you don’t need a test to explicitly check that the discount service its not called, probably you have more services in your applications and you don’t want to check that there is no calls to all those services.

All good unit test suites do not just test the sunshine cases, but also test to see what happens if something is done that is supposed to be disallowed.

You’ll have to make an exhaustive list of cases that your unit has to be able to handle.
For every one of your casetypes in the snippet you posted, you’ll need to think of at least one case that has to evoke the error handling code in your code for every error handled by your code. For the sunshine cases it matters how much your ‘input range’ is. You’ll have to test for every class of input in the range you’re going to handle.

After you’ve done that, you have to determine which ones are worth working out for complete testing.

Your not calling 4 or 5 should definitely be one of several test cases.

BTW, I agree with @MichaelT and I’d suggest using a strategy pattern [1] to get a better design going. This would also make it easier to build a proper test suite for your application, as all behaviours are in their own classes.

[1]http://en.wikipedia.org/wiki/Strategy_pattern

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật