Pirkei Avot 5:4 states that Avraham was given 10 tests, each of which he passed. According to most countings, two of the tests are in Perek 12. One is the command to Avram to leave Charan and go to Canaan, and the other is that soon after he got to Canaan a famine forced him to leave and go to Mitzrayim.
As Avram and his wife Sarai were nearing Mitzrayim, Avram told Sarai to claim that she was his sister not his wife. When they do this, the Mitzrim take Sarai and give her to Paroh as a wife.
Three incidents like this one are recorded in Sefer Bereishit, and each time the king gets upset and claims that he would never have taken the woman if he knew she was already married. The first time is here, the second is with Avimelech in Perek 20, and the third time is with Rivka and Avimelech in Perek 26.
There are two different ways to look at the Avot and other figures in Tanach. One is to view them as perfect and to view each of their actions as righteous and wise. The other view is that while these people were great Tzaddikim, they were still human and sometimes made mistakes. According to the Ramban, the actual test was how Avram would treat Sarai in Mitzrayim, and the Ramban believes that Avram failed this test. Avram should have said that Sarai was his wife, but he put his own life first instead and did not have enough trust in Hashem.
How can we view this story today? According to Halacha, it is better to die than to engage in adultery, and Avram was putting Sarai in a situation where she would likely be forced to engage in adultery with Paroh. However, Rav Hirsch defends Avram: by saying that Sarai was his sister, Avram hoped to prolong the time before Paroh slept with Sarai, and perhaps this would give Avram time to come up with another plan.
There is a powerful lesson to be learned either way. According to Rav Hirsch, we are required to delay bad events because we may find ways to circumvent them. According to the Ramban, one should keep his wife close to him and must put his wife’s honor before his own life.