Why do plants fruit?
Seeds are spread by many mechanisms 

Wind dispersal: When air currents used to spread seeds. Often these plants have evolved features to facilitate wind catching, for example dandelions. Aka, anemochory. 

Propulsion &amp; bursting: When seeds are propelled from the plant in an such as in these videos. This is called Ballochory.

Water: Similarly to wind dispersal plants can spread seeds by water movement/currents, aka Hydrochory. This is used by many algae and water living plants.

Sticky Seeds: There are many ways a seed can attach to the outside of an animal - by using hooks, barbs, sticky excretions, hairs. Seeds then get carried by an animal and fall off later. This is epizoochory.

Fruiting: Plants can use seed-bearing fruit to encourage animals to eat the seeds. They will then be spread when the waste is excreted after digestion. This is a process of endozoochory. 

More than one way to spread a seed

From above you can see there are many ways to spread a seed. This means that many plants (those that do not use fruit) will want (from an evolutionary perspective) to avoid being eaten at all costs, and therefore evolve poison/taste cues that reduce the frequency of this. However, if investing in producing fruit bears a net benefit, ie many seeds are spread and successfully propagate, then the cost of fruit production is not a risk. A fruiting plant can minimize the risk by limiting reward (making fruit taste good and be nutritious) to only the right spatiotemporal moment when the seeds are primed for dispersal, if the fruit is eaten before the seeds are ready/present in the fruit then there will be no benefit of producing the fruit. (I expect this is why unripe fruit doesn't tend to taste so nice - it doesn't taste nice unless it's ripe because those where mutations made fruit taste good before the seeds were ready had no offspring, and those that time the ripening well were much more successful).

Producing an edible fruit is not necessarily the best way but is the way many plant species have evolved to spread their seeds. Evolution works by random mutation and selection, which can only work with the variation it has available - only if a mutation to produce fruit arises in a species and it improves fitness is it then likely to spread. It appears that endozoochory could be a very good methods of long distance dispersal.

Some interesting stuff

Interestingly, according to this blog post, unripe fruits taste bitter because they are full of tanins. Cranberries are an odd fruit because they taste bitter which means they are unattractive to animals, but they do float - which is a good dispersal method - because of an air-pocket inside the fruit that evolved. It could be the case then that once a mutation arrived for the air-pocket the fruit could just float away on water to a new location, and then using resources to make the fruit sweet and tasty became an unnecessary cost so was lost (to some degree).

This article contains this text:


  Like many species of plants native to North America, the cranberry is specially adapted to wetlands -- water-soaked areas that create transition points between dry land and open water. But cranberries do not grow directly in the water. Instead, they typically lay their roots at its edge, taking advantage of the rich soils created by alternating layers of peat, sand, clay and rock.
  Other fruits are dependent on hungry creatures to spread their seed,
  so sweetness can help their chance of being eaten and transported
  elsewhere. Not cranberries. Because of their little air pockets, the
  fruit falls from the plant's long vines when ripe and lets the water
  transport it to distant beds.
  
  This advantage dictated the fruit we eat today.
  
  Blueberries and cranberries are close cousins and are in fact not
  berries at all; they belong instead to a class of fruits known as
  epigynous or false berries. Unlike a true berry, the fruit grows from
  beneath the rest of the flower parts and as the fruit ripens the
  flower stays attached and ripens as well.
  
  Vorsa believes that at some point during the last ice age there were
  fewer animals to eat the fruits, and this might have driven the two
  species to diverge. The cranberries developed an acidity level five
  times higher than blueberries because it came to rely on water for
  seed dispersal. Cranberries did not need to produce large amounts of
  sugar that their blue-colored relatives required to entice consumption
  by animals in order to spread their seeds.