RSpec无法显示消息

时间:2014-01-06 16:00:41

标签: ruby-on-rails ruby rspec

我在RSpec中进行测试,无法弄清楚为什么我会一直收到此错误消息:

 → rspec
  

.F.F。[已弃用] I18n.enforce_available_locales将来默认为true。如果您确实想跳过语言环境的验证,可以设置I18n.enforce_available_locales = false以避免此消息。   ...

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)查看单个事件会显示价格非零的事件的价格        失败/错误:期望(页面).to have_text(“$ 10.00”)          预计会在“BugSmash”中找到文本“$ 10.00”一个有趣的捣蛋之夜!当2014-&gt; 01-16 15:42:35 UTC哪里有丹佛价格免费所有活动“        #./spec/features/show_event_spec.rb:21:in'块(2级)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 %>

4 个答案:

答案 0 :(得分:3)

如果您想查看价格,#free?方法需要进行某种比较。

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

def free?
  price.to_i.zero?
end

希望有所帮助。

答案 1 :(得分:1)

def free?
    true
end

您的方法始终返回true。我建议你写一些像

这样的东西
def free?
    price == 0
end

只要您没有负价:)

答案 2 :(得分: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

我认为这就是它的全部内容。

答案 3 :(得分:0)

class Event
  def free?
    price > 0
  end
end

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