2014-01-06 86 views
1

我在RSpec的测试并不能弄清楚为什么我不断收到此错误信息:RSpec的失败,显示消息

→ 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 

这里是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 

这里是我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 

这里是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 %> 

和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 %> 
+0

向我们展示您的型号代码 – Agis

+0

发布。谢谢。 – Waymond

回答

3

如果要检查价格,需要在#free?方法中进行某种比较。

您可以通过转换为整数然后与零进行比较来同时检查空白和0。

def free? 
    price.to_i.zero? 
end 

希望有帮助。

1
def free? 
    true 
end 

你的方法总是返回true。我建议你应该写类似

def free? 
    price == 0 
end 

只要你没有负价格:)

+0

更改并获得更多错误消息。你说什么是有道理的。 – Waymond

+0

我试过price.blank? || price.zero?太...得到相同的错误信息。 – Waymond

0

注:我不熟悉使用Rails。所以我可能会在这里错过一些东西。您的event.rb不会做任何检查。请调整它。

举的代码应该怎么看起来像一个例子:

class Event 
    def free?(int) 
     int > 0 
    end 
end 

x = Event.new 
p x.free?(1).inspect # true 
p x.free?(0).inspect # false 

我想这是所有有给它。

+0

'int> 0'将返回'true'或'false',三元运算符是不必要的 – hawk

+0

@hawk你是对的。为了以防万一,我调整了代码。 –

0
class Event 
    def free? 
    price > 0 
    end 
end 

但是您必须确定价格在数据库中存储为整数,而不是字符串或null。因此,将它作为表中事件的默认价格设为0。并强制验证。

相关问题