Tuesday, October 23, 2012

MC Complete's Guide for the Perplexed, Part 1: Prayer

Daniel: I don’t get this whole prayer business.

Isaac: Prayer is praise, requests, and thanks.  What is there not to get?

Daniel: What about the requests?  What is the sense in asking Hashem to do things for me?  Doesn’t He already know what I want?

Isaac: Yes, he knows what you want.  But most of the time, you don’t ask for the things you want.

Daniel: I don’t?  What do you mean?

Isaac: Every Jew has his own specific set of desires.  But we all say the same Shmone Esrei.

Daniel: That may be, but how does that solve anything?  Doesn’t Hashem know what I’m going to say?

Isaac: Of course not.

Daniel: What?  What do you mean?

Isaac: Hashem knows the text of the Shmone Esrei very well.  He knows it by heart.  But he doesn’t know whether you are going to say it.

Daniel: Wait a minute.  Are you saying that God doesn’t know the future?

Isaac: No, it has nothing to do with that.  All I mean to say is that if you say the Shmoneh Esrei you say it, and if you don’t say it, you don’t say it.  God’s knowledge of the future, as well as His knowledge of the text, is irrelevant.

Daniel: Are you saying that Hashem want me to pray so that I focus on the needs of the community rather than my own?

Isaac: Exactly.  Better to pray for the needs of the community and take care of yourself, than to pray for your own needs and take care of the community.

Daniel: OK fine, let’s take one of the blessings of the Shmoneh Esrei.  Let’s take Refaenu.  In Refaenu, I ask Hashem -- we all ask Hashem -- to heal the sick Jews.

Isaac: Right.

Daniel: Does my prayer make it more likely that Hashem will heal the sick Jews?

Isaac: Hashem will heal the sick Jews.  We know that for certain.

Daniel: Well, I suppose we know (almost) for certain that He will heal some, but we also know for certain that He won’t heal them all, at least until the Moshiach comes.

Isaac: And maybe not even then.

Daniel: OK, so does my prayer make it more likely that a greater percentage of the sick Jews will be healed?

Isaac: We don’t know.

Daniel: I mean, on what basis does Hashem decide what percent will be healed?  What factors enter into these kinds of decisions?  What motivates Hashem?  What is He trying to achieve?  Why did he make people sick in the first place?

Isaac: We don’t know.

Daniel: So what is the significance of my prayer?

Isaac: Hashem cares about you.  You are asking Him to heal the sick Jews.  He will decide what percentage, based on His own calculations.  That’s all we know, and all we need to know.

Daniel: OK, I don’t know if you answered my question, but you certainly confused me enough that I no longer remember what my question was.

Tuesday, October 16, 2012

Mocks: A Guest Post by Zach Lloyd

So, you wrote a test that uses mocks where you could have used real dependencies...

I have a few major issues with this style of test:

  • It tests implementation, not behavior.  Why should a test care what methods are called within a function being tested so long as the function behaves in accordance with its contract?  With these kinds of tests it’s often possible to change a function to use a completely equivalent implementation, and yet the test fails after the change.  In the extreme, test cases like this end up purely mimicking the implementation of the method under test (e.g. if the method being tested calls x(), y(), z() on its dependency, then you will see that exact same sequence of calls in the mock setup).  This ends up testing nothing at all.
  • You can't even write a test with mocks like this without looking into the implementation of the class under test, which adds a lot of complexity to test writing and maintenance.
  • It takes me a non-trivial amount of mental effort to read one of these test cases with mocks -- much more than reading regular code.
  • I've never believed the argument that it's somehow bad to test real code.  Unless the dependency is truly on something that's hard to test reliably (like a network), I think you absolutely should be testing the real dependency by default.  I know the testing people disagree with this, but I don't particularly care.  The tests should be on an environment as similar to the real application as possible IMO.

I do feel pretty strongly that tests that use mocks like this are bad, but I'll leave it up to you whether you want to change it.