Reputation: 257
I'm testing in RSpec and can't figure out why I keep getting this error message:
→ rspec
.F.F.[deprecated] I18n.enforce_available_locales will default to true in the future. If you really want to skip validation of your locale you can set I18n.enforce_available_locales = false to avoid this message. ...
Failures:
1) An event is not free if the price is positive
Failure/Error: expect(event).not_to be_free
expected free? to return false, got true
# ./spec/models/event_spec.rb:13:in `block (2 levels) in <top (required)>'
2) Viewing an individual event shows the price for an event with a non-zero price Failure/Error: expect(page).to have_text("$10.00") expected to find text "$10.00" in "BugSmash A fun evening of bug smashing! When 2014->01-16 15:42:35 UTC Where Denver Price Free All Events" # ./spec/features/show_event_spec.rb:21:in `block (2 levels) in '
Finished in 0.39876 seconds
8 examples, 2 failures
Failed examples:
rspec ./spec/models/event_spec.rb:10 # An event is not free if the price is positive
rspec ./spec/features/show_event_spec.rb:16 # Viewing an individual event shows the price for an event with a non-zero price
Randomized with seed 37892
Here is event_spec.rb:
require 'spec_helper'
describe "An event" do
it "is free if the price is $0" do
event = Event.new(price: 0)
expect(event).to be_free
end
it "is not free if the price is positive" do
event = Event.new(price: 10)
expect(event).not_to be_free
end
end
and here is my show_event_spec.rb:
require 'spec_helper'
describe "Viewing an individual event" do
it "shows the event's details" do
event = Event.create(event_attributes)
visit event_path(event)
expect(page).to have_text(event.name)
expect(page).to have_text(event.location)
expect(page).to have_text(event.description)
expect(page).to have_text(event.start_at)
end
it "shows the price for an event with a non-zero price" do
event = Event.create(event_attributes(price: 10.00))
visit event_path(event)
expect(page).to have_text("$10.00")
end
it "shows 'Free' for an event with a zero price" do
event = Event.create(event_attributes(price: 0.00))
visit event_path(event)
expect(page).to have_text("Free")
end
end
here is the event.rb:
class Event < ActiveRecord::Base
def free?
price.blank? || price.zero?
end
end
index.html.erb:
<h1><%= pluralize(@events.size, 'Event') %></h1>
<% @events.each do |event| %>
<article>
<header>
<h2><%= link_to event.name, event %></h2>
</header>
<p>
<%= truncate(event.description, length: 35, separator: ' ') %>
</p>
<table>
<tr>
<th>When:</th>
<td><%= event.start_at %></td>
</tr>
<tr>
<th>Where:</th>
<td><%= event.location %></td>
</tr>
<tr>
<th>Price:</th>
<td><%= number_to_currency(event.price) %></td>
</tr>
</table>
</article>
<% end %>
and show.html.erb:
<article>
<header>
<h1><%= @event.name %></h1>
</header>
<p>
<%= @event.description %>
</p>
<h3>When</h3>
<p>
<%= @event.start_at %>
</p>
<h3>Where</h3>
<p>
<%= @event.location %>
</p>
<h3>Price</h3>
<p>
<%= format_price(@event) %>
</p>
</article>
<%= link_to "All Events", events_path %>
Upvotes: 1
Views: 581
Reputation: 1486
NOTE: I'm not familiar with Rails. So I might be missing something here.
Your event.rb
doesn't do any check. Please adjust it.
To give an example of how the code should look like:
class Event
def free?(int)
int > 0
end
end
x = Event.new
p x.free?(1).inspect # true
p x.free?(0).inspect # false
I think that's all there is to it.
Upvotes: 0
Reputation: 4633
class Event
def free?
price > 0
end
end
But you have to be sure that the price is stored as integer in the database, not as a string or null. So make it a 0 as default price for events in the table. And force validation.
Upvotes: 0
Reputation: 1515
If you want to check the price, a comparison of some kind is needed in the #free?
method.
You can check for blank and 0 at the same time by converting to an integer and then comparing with zero.
def free?
price.to_i.zero?
end
Hope that helps.
Upvotes: 3
Reputation: 1502
def free?
true
end
Your method always returns true. I suggest you should write something like
def free?
price == 0
end
As long as you don't have negative prices:)
Upvotes: 1