I have a Panel class which has an array. All instances of that class have to contain one of the elements of that array.
TEXT_ONLY = "text_only"
IMAGE_LEFT = "image_left"
IMAGE_RIGHT = "image_right"
IMAGE_ONLY = "image_only"
VIDEO = "video"
PANEL_TYPE = [TEXT_ONLY, IMAGE_LEFT, IMAGE_RIGHT, IMAGE_ONLY, VIDEO]
validate :panel_type, inclusion: PANEL_TYPE.each
I am trying to write a test that matches this validation.
mandatory_string_from_collection :panel_type, CorporatePanel::PANEL_TYPE.each
def mandatory_string_from_collection(method,collection)
context "#{method} is a mandatory string" do
exists(method)
it "should reject a blank #{method}" do
@instance.send("#{method}=","")
expect(@instance).not_to be_valid
end
context "where the value is not within the acceptable options" do
it "should reject it" do
@instance.send("#{method}=","zgodnflax")
expect(@instance).to_not be_valid
end
end
context "where the value is within the acceptable options" do
it "should accept it" do
@instance.send("#{method}=",collection.first)
expect(@instance).to be_valid
end
end
end
end
Here is the factory
this i my factory
factory :corporate_panel do
corporate_page
section 1
panel_type "video"
title "MyString"
headline "MyString"
body "MyText"
# workflow_state "MyString"
end
the test yields this, and i do not understand why?!
1) CorporatePanel panel_type is a mandatory string should reject a blank panel_type
Failure/Error: expect(@instance).not_to be_valid
expected #<CorporatePanel id: 353, corporate_page_id: 464, section: 1, position: 1, panel_type: "", title: "MyString", headline: "MyString", body: "MyText", workflow_state: "draft", created_at: "2014-08-11 22:49:26", updated_at: "2014-08-11 22:49:26", asset_id: nil> not to be valid
# -e:1:in `<main>'
2) CorporatePanel panel_type is a mandatory string where the value is not within the acceptable options should reject it
Failure/Error: expect(@instance).to_not be_valid
expected #<CorporatePanel id: 355, corporate_page_id: 466, section: 1, position: 1, panel_type: "zgodnflax", title: "MyString", headline: "MyString", body: "MyText", workflow_state: "draft", created_at: "2014-08-11 22:49:26", updated_at: "2014-08-11 22:49:26", asset_id: nil> not to be valid
eachin the spec doesn't make sense either. not where you've got it... you aren't iterating through thePANEL_TYPEs the way you've written it... I think you might misunderstand whateachactually does.