Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 保存参考自我后的Rails rspec_Ruby On Rails_Ruby_Rspec - Fatal编程技术网

Ruby on rails 保存参考自我后的Rails rspec

Ruby on rails 保存参考自我后的Rails rspec,ruby-on-rails,ruby,rspec,Ruby On Rails,Ruby,Rspec,我想在我的网站上添加亵渎检查 我采用TD方法,并尝试以下方法: 检查特定配置文件字段中是否存在亵渎 创建一个标志 如果不存在标志,则创建标志 创建一个标志(如果存在但已被取消) 以下是我目前的规格: describe Painter do before do @painter = FactoryGirl.create(:painter_flag) end context "blacklist flag" do it "check if profanity exist

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

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

  • 检查特定配置文件字段中是否存在亵渎
  • 创建一个标志
  • 如果不存在标志,则创建标志
  • 创建一个标志(如果存在但已被取消)
  • 以下是我目前的规格:

    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
    
    油漆工相关代码:

    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
    

    我认为导致错误的原因是回调是在
    @painter=FactoryGirl.create(:painter\u flag)
    行上执行的


    如果要测试回调,可能需要使用
    FactoryGirl.build
    方法。

    只需在工厂中为
    :painter\u标志使用此行,而不使用
    =
    符号

    skills "badword"
    

    您的
    :painter_flag
    工厂似乎在创建对象时未设置
    技能
    属性/关联。你能发布代码吗?嗨,德帕,请看上面的更新。很好,有用吗?如果答案正确,请别忘了选择正确的答案。
    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
    
    skills "badword"