Our Lord, through His goodness, may wish to represent Himself to a soul so that it might know or love Him more, or that He might show it one of His secrets, or grant it some particular gifts or favors. And if the soul, as I have said (The Foundations, 8.3), considers itself a saint because of a favor (for it should be confounded and know how little its lowliness deserves any favor) and thinks that this favor comes to it because of some service it has rendered, clearly the great good that could result is converted into evil, as in the example of the spider.
Well now, let us suppose that the devil so as to incite pride causes these apparitions. The soul may think they are from God, humble itself, recognize its unworthiness to receive so great a favor, and strive to serve more. For in seeing itself rich, while not even deserving to eat the crumbs that fall from the table (Cf. Mt 15:21-28), of the persons of whom it has heard that God grants these favors (I mean, not deserving to be a servant of any of them), it humbles itself, begins to force itself to do penance, prays more, and takes greater care not to offend this Lord. For it thinks it is He who grants this favor, and obeys with greater perfection.
If it responds in these ways, I am sure the devil will not return, but will be put to shame, and that no harm will be left in the soul.
Saint Teresa of Avila
The Foundations, 8.4
Teresa of Avila, St. 1985, The Collected Works of St. Teresa of Avila, translated from the Spanish by Kavanaugh, K; Rodriguez, O, ICS Publications, Washington DC.