我已经编写了一个 rspec 测试来检查我的永久链接中的无效字符:
describe "formatting permalinks when creating a page" do
it "does not allow crazy characters" do
page = create(:page, permalink: '#$%^&*first-title')
expect(page).to have(1).errors_on(:permalink)
end
end
在我的page.rb
模型中,我实施了此验证以使其通过:
class Page < ActiveRecord::Base
validates :permalink, format: {:with => /\A[a-zA-Z-]+\Z/, :on => :save!}
before_create :create_slug
def create_slug
self.permalink = self.permalink.parameterize
end
end
但我得到了他的错误:
expected 1 errors on :permalink, got 0
我究竟做错了什么?我该如何解决?