新手问题 测试相关的问题

sanvi · September 25, 2012 · Last by ottoyes replied at February 22, 2013 · 2154 hits

看了这么多案例,貌似比较推荐是 Rspec+factory-girl

我尝试了一下照着 ruby-china 的代码弄了下

# coding: utf-8
require 'spec_helper'

describe Location do
    before(:each) do
        @location = Factory(:location,:name => 'foo')
    end

    it "should validates_uniqueness_of :name ignore case" do
        Location.create(:name => "FoO").should have(1).error_on(:name)
    end
end

但是他说找不到 Factory,那我应该在那配置这个 factory-girl 呢 错误信息

Failures:

  1) Location should validates_uniqueness_of :name ignore case
     Failure/Error: @location = Factory(:location,:name => 'foo')
     NoMethodError:
       undefined method `Factory' for #<RSpec::Core::ExampleGroup::Nested_1:0x007ffdfeb93ad0>
     # ./spec/models/location_spec.rb:6:in `block (2 levels) in <top (required)>'

我看了很久都不明白

  1. should 的行为需要取决于后面的动词
  2. 一眼看不出来 error_on 的对象是什么

这样的 DSL 为什么会受欢迎。

#1 楼 @Rei 刚修改了错误信息,那你认为要如何来做测试好

#2 楼 @sanivbyfish Unit::Test 而不是 Rspec

我也碰到了这个问题,请问楼主最后是怎么解决的?

You need to Sign in before reply, if you don't have an account, please Sign up first.