我正在构建一个每日交易轨道应用程序,并且我按照 M. Hartl 教程设置了一些 rspec 测试。
对于用户来说,它们工作得很好。
但是现在我已经将它用于模型 Eals 并且所有的都通过了,当他们不应该的时候。例如,在我的模型中,我将标题设置为不能超过 200 个字符(注意:在我看来,当我尝试将标题设置得比这更长时,它会起作用并提醒我这是不可能的)
但是,当我进行测试时,无论我在标题测试中尝试使用 long = "a" * 50、a * 201 甚至是 * 10000 进行标题字符长度测试,它总是通过!有一个大问题我没能找到。实际上所有其他测试都有相同的问题:它们总是通过!
这是我的模型/deal.rb
class Deal < ActiveRecord::Base
belongs_to :admin_user
attr_accessible :url_path,
:country,
:title,
:description,
:twitter_msg,
:admin_user_id
validates :url_path,
presence: true,
uniqueness: { :case_sensitive => false }
validates :country,
:inclusion => { :in => ['France', 'Germany', 'United States'],
:message => "%{value} is not a valid country. " }
validates :title,
presence: true,
length: { maximum: 200,
:message => "Your title has %{value} characters but must be shorter than 200 characters" }
validates :description,
presence: true,
length: { maximum: 500,
:message => "Your title has %{value} characters but must be shorter than 500 characters" }
validates :twitter_msg,
presence: true,
uniqueness: { :case_sensitive => false }
validates :admin_user_id, presence: true
还有我的 deal_spec.rb:
require 'spec_helper'
describe Deal do
let(:admin_user) { FactoryGirl.create(:admin_user) }
before (:each) do
@attr = { url_path: "lorem ipsum",
country:"France",
title: "lorem ipsum",
description:"lorem ipsum",
twitter_msg:"lorem ipsum",
}
end
it { should respond_to(:url_path) }
it { should respond_to(:country) }
it { should respond_to(:title) }
it { should respond_to(:description) }
it { should respond_to(:twitter_msg) }
describe "title test" do
it "should reject deals with title that is too long" do
long = "a" * 50
hash = @attr.merge(:title => long)
Deal.new(hash).should_not be_valid
end
[other tests]
end #end of title test
如果有人可以帮助我理解这一点,那就太好了,我已经花了几个小时没有任何线索。
在听从某人的建议后,我改变了我的测试
Deal.new(hash).should have(1).error_on(:title)
describe "test" do
it "should reject games with title that is too long" do
long = "a" * 250
hash = @attr.merge(:title => long)
Game.new(hash).should have(1).error_on(:title)
end
end
但现在它一直在过去,即它告诉我,无论我是否输入 long= "a" * 5, long="a" * 300...