[英]rspec rails, instance should match an element from an array
I have a Panel class which has an array. 我有一个具有数组的Panel类。 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
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) 您正在验证
panel_type
是否在集合PANEL_TYPE
(也叫PANEL_TYPES
因为集合中有多个)
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: 如果您想要的是“我想检查每个可以使用的
PANEL_TYPE
”,那么您可能想要的是:
CorporatePanel::PANEL_TYPE.each do |panel_type|
mandatory_string_from_collection :panel_type, panel_type
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.