感谢您的时间。 我的RSpec规范中有一个有趣的逻辑错误。 下面我提供了两个例子,第一个例子总是失败,而第二个传递。
例如:下面的代码将创建一个期望为3但是返回1的失败规范
it "must not have the same question twice" do exam = Exam.new q1 = Question.new(question: "What color is the sky?") q2 = Question.new(question: "What color is the sky?") q3 = Question.new(question: "What sound does a dog make?") q4 = Question.new(question: "What sound does a dog make?") q5 = Question.new(question: "hello world?") exam.questions.push(q1,q2,q3,q4,q5) expect(exam.questions.uniq.size).to eql(3) end但是当我使用下面的代码时,用常规数组替换Exam对象,它按预期工作并传递规范。
it "must not have the same question twice" do exam = [] q1 = Question.new(question: "What color is the sky?") q2 = Question.new(question: "What color is the sky?") q3 = Question.new(question: "What sound does a dog make?") q4 = Question.new(question: "What sound does a dog make?") q5 = Question.new(question: "hello world?") exam.push(q1,q2,q3,q4,q5) expect(exam.uniq.size).to eql(3) end以下是我的问题对象中的overrode方法示例
def ==(other) question == other.question end def eql?(other) other.is_a?(self.class) && question.eql?(other.question) end def hash question.hash endThank you for your time. I have an interesting logical error in my RSpec specification. Below I have provided two examples, the first one always fails whilst the second passes.
For example: The below code will create a failing specification expecting 3 but getting back 1
it "must not have the same question twice" do exam = Exam.new q1 = Question.new(question: "What color is the sky?") q2 = Question.new(question: "What color is the sky?") q3 = Question.new(question: "What sound does a dog make?") q4 = Question.new(question: "What sound does a dog make?") q5 = Question.new(question: "hello world?") exam.questions.push(q1,q2,q3,q4,q5) expect(exam.questions.uniq.size).to eql(3) endHowever when I use the below, replacing the Exam object with a regular array it works as intended and passes the specification.
it "must not have the same question twice" do exam = [] q1 = Question.new(question: "What color is the sky?") q2 = Question.new(question: "What color is the sky?") q3 = Question.new(question: "What sound does a dog make?") q4 = Question.new(question: "What sound does a dog make?") q5 = Question.new(question: "hello world?") exam.push(q1,q2,q3,q4,q5) expect(exam.uniq.size).to eql(3) endHere is a sample of my overrode methods inside my question object
def ==(other) question == other.question end def eql?(other) other.is_a?(self.class) && question.eql?(other.question) end def hash question.hash end最满意答案
exam.questions.uniq正在删除ActiveRecord :: Associations :: CollectionProxy的重复项。
exam.uniq正在删除数组的重复项。
如果查看ActiveRecord 源,您可以看到记录ID用于确定唯一性而不是相等性。
def distinct seen = {} load_target.find_all do |record| seen[record.id] = true unless seen.key?(record.id) end end alias uniq distinctexam.questions.uniq is removing duplicates for an ActiveRecord::Associations::CollectionProxy.
exam.uniq is removing duplicates for an Array.
If you look at the ActiveRecord source you can see that record ID is used to determine uniqueness instead of equality.
def distinct seen = {} load_target.find_all do |record| seen[record.id] = true unless seen.key?(record.id) end end alias uniq distinct更多推荐
发布评论