2014-01-06 113 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。並強制驗證。

相關問題