mercredi 27 avril 2016

Unit test bad practice? Create an interface to mock inner method?

I have a class with a few private methods which are called at different places of my code. My goal is to be able to write short tests where I'm not repeating all which is implied by those private and often reused methods. Just making sure they're called properly is enough (and later on test them on their own).

My first guess to achieve this was to find good reason to put those method elsewhere, in a well design (SRP) class. But the logic inside those private method really belong to the whole class responsibility, so putting them elsewhere might be awkward (and tedious?).

Thus, I though I could create an interface for those method (which thus become public), make all calls to an instance implementing that interface and the instance is actually the class instance itself (aka: this)

C#

    internal ICandidateAssist CandidateAssist { private get; set; }
    public CandidateController()
    {
        CandidateAssist = this;
    }

    void ICandidateAssist.ComputeSelected(Candidate candidate)
    {
        ...
    }

    public ActionResult Edit(int id)
    {
        var candidate = ...
        ...
        ResourceAssist.ComputeSelected(candidate);
        ...            
    }

Am I missing a better way to handle this situation?

Aucun commentaire:

Enregistrer un commentaire