0

我想在我的网站上添加亵渎检查。

我正在采用 TD 方法,并且正在尝试以下方法:

  1. 检查特定个人资料字段中是否存在亵渎
  2. 创建一个标志
  3. 如果不存在则创建一个标志
  4. 如果存在,则创建一个标志,但已被驳回

到目前为止,这是我的规格:

describe Painter do
  before do
    @painter = FactoryGirl.create(:painter_flag)
  end

  context "blacklist flag" do
    it "check if profanity exists" do
      @painter.experience = "test"
      @painter.save
      expect {@painter.blacklist_flags?}.to be_true
    end
    it "create flag if profanity exists" do
      @painter.experience = "test"
      @painter.save
      BlacklistFlag.count.should be > 0
    end
  end
end

Painter相关代码:

after_save :create_flag, if: :blacklist_flags?

def blacklist_flags?
    list = ""
    list << skills
    #list << experience
    #list << first_name
    #list << last_name
    #list.downcase.scan(/(badword|badword)/).size > 0
  end

def create_flag
end

如果我在两个测试通过上面注释掉以下代码:

 list << skills

当我留下代码时,我收到以下错误:

2) 如果存在亵渎,则 Painter 黑名单标志创建标志失败/错误:@painter = FactoryGirl.create(:painter_flag) 类型错误:无法将 nil 转换为字符串

引用自我似乎存在问题,因为技能、经验等是模型的一部分。我不知道如何解决这个问题。请指教。

更新:

FactoryGirl.define do
  factory :painter do
    first_name "Brian"
    last_name  "Rosedale"
    state "OH"
    zip_code "43081"
    sequence(:email) {|n| "nobody#{n}@painterprofessions.com" }
    phone "12345566"
    pdca_member false
    password "123456"
    factory :painter_flag do
       skills = "badword"
    end
  end
end
4

2 回答 2

0

I think what's causing the error is because the callback is been executed on the line @painter = FactoryGirl.create(:painter_flag).

You might want to use FactoryGirl.build method if you want to test the callback.

于 2013-09-26T01:34:00.997 回答
0

只需在您的工厂使用这条生产线:painter_flag,无需=标志。

skills "badword"
于 2013-09-26T03:08:54.217 回答