0

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
4
  • what's the "each" for? (in the model-class, not the spec) Commented Aug 12, 2014 at 4:18
  • @TarynEast to test against each element of the array Commented Aug 12, 2014 at 9:39
  • as i said - not the each in the spec... the each in the model. I'm pretty sure that shouldn't be there. Commented Aug 12, 2014 at 23:18
  • In fact - the each in the spec doesn't make sense either. not where you've got it... you aren't iterating through the PANEL_TYPEs the way you've written it... I think you might misunderstand what each actually does. Commented Aug 12, 2014 at 23:19

1 Answer 1

1

This

validate :panel_type, inclusion: PANEL_TYPE.each

should just be

validate :panel_type, inclusion: PANEL_TYPE

You are validating that panel_type is in the set PANEL_TYPE (also, I'd call it PANEL_TYPES because there's more than one in the set)

Secondly, you do not want to do this each either

mandatory_string_from_collection :panel_type, CorporatePanel::PANEL_TYPE.each

if what you want is "I want to check that for each PANEL_TYPE that it is ok to use" then what you probably want is this:

CorporatePanel::PANEL_TYPE.each do |panel_type|
  mandatory_string_from_collection :panel_type, panel_type
end
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.