2016-04-12 16 views
1

rails4、factory_ girl、rspec、shoulda matchersを使用しています。私は以下のコードとRSpecのを実行すると、私はこのエラーを取得する:RSpecのウェブサイトフォーマット検証に失敗しました

Product should validate that :website cannot be empty/falsy, producing a custom validation error on failure 
Failure/Error: self.website = "http://#{self.website}" unless self.website[/^https?/] 

NoMethodError: 
    undefined method `[]' for nil:NilClass 

私はformat_website方法からunless self.website[/^https?/]を削除した場合、私はこのエラーを取得する:

Product did not properly validate that :website cannot be empty/falsy, 
    producing a custom validation error on failure. 
    After setting :website to ‹nil› -- which was read back as ‹"http://"› 
    -- the matcher expected the Product to be invalid and to produce a 
    validation error matching ‹/can't be blank/› on :website. The record 
    was indeed invalid, but it produced these validation errors instead: 

    * user: ["can't be blank"] 
    * name: ["can't be blank"] 
    * company: ["can't be blank"] 

私はこの仕事をするために何をすべき?

製品モデル

belongs_to :user 

validates :name, presence: { message: "can't be blank" }, length: { maximum: 140, message: "can't be longer than 140 characters" }, uniqueness: { message: "already exists" } 
validates :company, presence: { message: "can't be blank" }, length: { maximum: 140, message: "can't be longer than 140 characters" } 
validates :website, presence: { message: "can't be blank" }, length: { maximum: 140, message: "can't be longer than 140 characters" } 

before_validation :format_website 
validate :website_validator 


def format_website 
    self.website = "http://#{self.website}" unless self.website[/^https?/] 
end 

def website_validator 
    self.errors.add :website, "format is invalid!" unless website_valid? 
end 

def website_valid? 
    !!website.match(/^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-=\?]*)*\/?$/) 
end 

工場

FactoryGirl.define do 
    factory :product do 
    name { Faker::Commerce.product_name } 
    company { Faker::Company.name } 
    website { 'https://example.com' } 
    user 
    end 
end 

it { is_expected.to callback(:format_website).before(:validation) } #this one is not important, if I take out it still gives the same error 
it { is_expected.to validate_presence_of(:name).with_message(/can't be blank/) } 
it { is_expected.to validate_presence_of(:company).with_message(/can't be blank/) } 
it { is_expected.to validate_presence_of(:website).with_message(/can't be blank/) } 
it { is_expected.to belong_to(:user) } 

答えて

2

あなたはwebsitenilがないことを確認する必要があります。その場合

def format_website 
    return if website.blank? 

    self.website = "http://#{self.website}" unless self.website[/^https?/] 
end 

self.website == nil場合、あなたはそれゆえ最初のエラー、nilのオブジェクトのメソッドを呼び出す[]しようとするでしょう。後者の場合については

は、答えはRSpecのからあなたが持っているリターンである:ウェブサイトがnilであるからである "http://"を返し format_website

After setting :website to ‹nil› -- which was read back as ‹"http://"›

あなたの方法。

+0

born4new、私はこの質問を正しく質問しませんでした。私はこの問題を解決してバリデーターを動作させる方法を知りたいです。他の種類のバリデーションを使用すべきですか? –

+0

また、問題の原因となっている仕様を確認することなく、難しいとは言いませんが、FactoryGirlでモデルを作成するときに、 'website'属性をインスタンス化しないと確信しています。だから、それを修正する必要があります。 – born4new

+0

born4new、私は工場で私の質問を更新しました。 –

関連する問題