We don't know whether they evolved these chemicals to prevent being eaten by animals though. People have tested spicy (capsaicin based) foods with mice, and found that mice actually seemed to like or not really react to spicy foods. This means that capsaicin did not evolve to protect against rodents like initially thought.
In fact, we now know that capsaicin is a very powerful antifungal chemical. Chili peppers naturally grow in hot, humid environments where fungi thrive. There also aren't many rodents in those areas. So the spiciness experienced by mammals like humans is just a side effect that didn't really affect its evolution.
We don't know whether they evolved these chemicals to prevent being eaten by animals though. People have tested spicy (capsaicin based) foods with mice, and found that mice actually seemed to like or not really react to spicy foods. This means that capsaicin did not evolve to protect against rodents like initially thought.
In fact, we now know that capsaicin is a very powerful antifungal chemical. Chili peppers naturally grow in hot, humid environments where fungi thrive. There also aren't many rodents in those areas. So the spiciness experienced by mammals like humans is just a side effect that didn't really affect its evolution.