Depends on what you asked them to do. Some people self-diagnose a problem, usually incorrectly, then ask the mechanic to replace a specific part or perform a specific service. In that case, they did what you requested and you are obligated to pay for that.
If you handed over your car and described the problem, then asked them to diagnose and repair it, there is a gray area of, ... Well, ... Confusion, for lack of a better term. Typically, if they can figure out the problem, they will give you an estimate on cost, then perform the repair after you okay it. Very often there is no positive way to tell for sure what will solve the problem other than by performing the repair, then see if the problem is gone. "Trial and error" is the method we really hate but it's what we often have to resort to, especially with a car such as yours that has a lot of computers on it. The only practical way to test them is to replace them and that's expensive.
You didn't say what kind of problem you were having but with late model cars, the biggest percentage of problems are electrical because the engineers have designed them to be ridiculously complicated. If your mechanic suspects a sensor is intermittent or just defective, he has to order it from a supplier, and once it's installed, he can't return it. Normally if it doesn't solve the problem, they will take it back out and not charge you for it. They have to hope they can use that part in the future. They can't do that very often before all of their profit is sitting on the shelf, unsold. Imagine if you had to throw away one out of every two bags of groceries you brought home because no one wanted to eat what was in there. It wouldn't take long before you started being more careful about what you were buying. Likewise, the mechanic has to be really sure he has identified the correct parts to replace so he doesn't buy things he can't resell. That extra testing takes time and shops have to charge for their time just like any other business. They would rather not have to charge for that extra time so they usually jump on the most likely cause of the problem and start there. Most of the time their first attempt at solving the problem is correct but sometimes it can take two or three return visits before they find it. That is no different than visiting a doctor. Doctors rarely diagnose your ailment correctly the first time. You have to keep going back for more tests but you don't complain about being ripped off. Why are mechanics held to much higher standards?
Noises are another problem area. There are a lot of noises that can sound the same but have three or four causes. Some cars are notorious for steering and suspension parts that wear out so those are what most mechanics will head for first since they are so common. Once they realize those common failure parts didn't solve the noise they have to start investigating further. I've had trade-in cars that have taken DAYS with a helper to find the cause of a rattle. I've also had lemon law buybacks with brake pulls that took days to diagnose and it came down to just replacing parts until it was solved, then we knew which parts to inspect to determine why they caused the problem. The manufacturer that was paying the bill and insisted it be fixed did not expect us to remove all of the parts it didn't need. That would have taken more hours, but mechanics also understand they don't expect you to pay for all of those trial and error parts. They have to walk a tightrope between cost to you for the unneeded parts vs. Cost to you for the additional labor to remove them. If it's a common failure item, why take the new part back off when your old one is going to fail soon anyhow?
It all boils down to the type of problem you're having and what the mechanic recommended. If you can be more specific, so can I. If you are having an intermittent problem, all bets are off. There is no way anyone can know for sure if one of those are fixed unless it acts up again; then you know it's not fixed.
I had one car with an intermittent electrical problem, and the owner came back 11 times before I found it. He wasn't angry and he didn't accuse me of ripping him off. He was grateful I kept working on it.
Also be aware that a lot of multiple problems can be related. You might not feel a warped brake rotor or a loose tie rod end in the steering linkage, but the two together can cause the wheel to shake during braking. When the mechanic finds either one of those, he will assume that is the cause of the problem and the car will be fixed. Of course when the other part still causes a problem he is going to recommend more repairs. The only thing he is guilty of in that case is he didn't inspect it far enough the first time. He found something and quit looking too soon. That doesn't mean he's ripping you off. It means he didn't charge you enough the first time.