mardi 24 novembre 2015

What is the best approach to write unit tests for a method that calls into multiple methods?

I'm starting to develop in Go, and now i need to write unit tests for an existing project. (I know that write tests before is better, but the code was made before)

I need to test the follow method that encapsulates a main functionality, and this method calls multiple methods. What is the best approach to write this unit tests? It's better to split them into small tests and test the group, validateCalculation, convertItems and mergeDiscount, or test the whole method? Any help will be very welcome !!!

        func calculate(context context.Execution, calculation Calculation) (CalculationResult, error) {
        items, itemsTranslated, err := group(context, calculation)
        if err != nil {
            return CalculationResult{}, err
        }

        if err := validateCalculation(calculation, items); err != nil {
            return CalculationResult{}, err
        }
        vendorChannel := make(chan interface{}, len(items))
        defer close(vendorChannel)
        for vendor, value := range items {
            vendorCalculation := VendorCalculation{Vendor: vendor, Customer: calculation.Customer, Delivery: calculation.Delivery, Items: value, Payment: calculation.Payment}
            vendorChannel.Add(vendorChannel.Message{Id: seller, Type: channel.CALCULATION, Content: vendorCalculation, Created: time.Now(), Context: context, Callback: vendorChannel})
        }

        var calculationItem []CalculationItem
        discounts := make(map[string][]Discount)
        for i := 0; i < len(vendorItems); i++ {
            response := (<-vendorChannel).(CalculationResult)
            deliveryItems = append(deliveryItems, convertItems(context, response.Items, simulationInfo.Delivery, sellerSkuTranslate)...)
            mergeDiscount(discounts, response.Discounts)
        }
        return CalculationResult{Delivery: calculation.Delivery, Items: calculationItem, Discounts: discounts}, nil
    }

Aucun commentaire:

Enregistrer un commentaire