RSpec 期望和 ExpectationNotMetError
RSpec Expectations and ExpectationNotMetError
你好,我一直在学习一门课程来加深我对黄瓜的理解。这门课程有点旧,所以我不得不更新一些 rspec 语法,从 should to expect 但除此之外一直非常仔细地遵循它。我无法让最近的测试通过,我很不明白为什么。我收到的错误如下。我知道它应该收到 95 时却收到 nil 但我仍然不明白为什么。
When They submitted an assignment # features/step_definitions/teacher_grade_assignment.rb:6
Then the assignment has a grade # features/step_definitions/teacher_grade_assignment.rb:14
expected: 95
got: nil
(compared using ==)
(RSpec::Expectations::ExpectationNotMetError)
./features/step_definitions/teacher_grade_assignment.rb:15:in `/^the assignment has a grade$/'
features/teacher_can_grade_assignment.feature:11:in `Then the assignment has a grade'
Failing Scenarios:
cucumber features/teacher_can_grade_assignment.feature:7 # Scenario: Teacher can grade assignment
teacher_grade_assignment.rb
Given(/^I have a student$/) do
@teacher = Teacher.new
@student = Student.new
end
Given(/^They submitted an assignment$/) do
@teacher.submit_assignment(@student, Assignment.new)
end
When(/^I grade the assignment$/) do
@teacher.record_grade(@student, 95)
end
Then(/^the assignment has a grade$/) do
expect(@teacher.assignment_for_student(@student).grade).to eq(95)
end
student_assignment_steps.rb
Given(/^I am a student$/) do
@student = Student.new #setting as an instance variable to access later on
@teacher = Teacher.new
end
When(/^I submit an assignment to my teacher$/) do
@assignment = Assignment.new
@teacher.submit_assignment(@student, @assignment)
end
Then(/^my teacher should have my assignment$/) do
expect(@teacher.assignment_for_student(@student)).to eq(@assignment)
end
teacher.rb
class Teacher
def initialize
@assignments = {}
end
def record_grade(student, grade)
assignment = @assignments[student] #assignment equal to assignments of the student
assignment.grade = grade
@assignments[student] = assignment
end
def submit_assignment(student, assignment)
@assignments[student] = assignment
end
def assignment_for_student(student)
@assignments[student]
end
end
teacher_spec.rb
require_relative "../lib/teacher"
require "rspec"
describe Teacher do
it "should store assignments" do
student = double
assignment = double
subject.submit_assignment(student, assignment)
expect(subject.assignment_for_student(student)).to eq(assignment)
end
describe "should record a grade" do
it "should record and the grade" do
student = double
assignment = double
expect(assignment).to receive(:grade=).with(95)
subject.submit_assignment(student, assignment)
subject.record_grade(student, 95)
end
end
end
assignment_spec.rb
require_relative "../lib/assignment"
describe Assignment do
it "should store a grade" do
subject.grade = 60
expect(subject.grade).to eq(60)
end
end
assignment.rb
class Assignment
attr_accessor :grade
end
student.rb
class Student
end
teacher_can_grade_assignment.feature
Feature: Teacher can grade assignment
As a Teacher
I can grade my students' assignments
So that they can know their knowledge level
Scenario: Teacher can grade assignment
Given I have a student
And They submitted an assignment
When They submitted an assignment
Then the assignment has a grade
您的测试告诉您 @teacher.assignment_for_student(@student).grade
是 nil
。不知何故,它没有正确设置。正如您所说,这出乎意料,您希望它是 95。
在 TDD 中,最好的下一步是找出失败测试中的更多状态。
添加一些额外的(临时的)期望以查看失败步骤中对象的状态:
expect(@teacher.assignment_for_student(@student)).to eq(@assignment)
expect(@teacher.class_variable_get(:@assigments).to include(@assignment)
即使您确定会失败的测试通常也会有很大帮助。
expect(@teacher.assignment_for_student(@student)).to be "failure"
通过在你的步骤周围散布这样的期望,你可以调试状态并查看代码集成错误的地方。
根据您粘贴的代码,我没有立即发现代码有任何问题。
您的作业没有评分,因为老师从未给它评分:您的功能没有调用 "I grade the assignment step"
你好,我一直在学习一门课程来加深我对黄瓜的理解。这门课程有点旧,所以我不得不更新一些 rspec 语法,从 should to expect 但除此之外一直非常仔细地遵循它。我无法让最近的测试通过,我很不明白为什么。我收到的错误如下。我知道它应该收到 95 时却收到 nil 但我仍然不明白为什么。
When They submitted an assignment # features/step_definitions/teacher_grade_assignment.rb:6
Then the assignment has a grade # features/step_definitions/teacher_grade_assignment.rb:14
expected: 95
got: nil
(compared using ==)
(RSpec::Expectations::ExpectationNotMetError)
./features/step_definitions/teacher_grade_assignment.rb:15:in `/^the assignment has a grade$/'
features/teacher_can_grade_assignment.feature:11:in `Then the assignment has a grade'
Failing Scenarios:
cucumber features/teacher_can_grade_assignment.feature:7 # Scenario: Teacher can grade assignment
teacher_grade_assignment.rb
Given(/^I have a student$/) do
@teacher = Teacher.new
@student = Student.new
end
Given(/^They submitted an assignment$/) do
@teacher.submit_assignment(@student, Assignment.new)
end
When(/^I grade the assignment$/) do
@teacher.record_grade(@student, 95)
end
Then(/^the assignment has a grade$/) do
expect(@teacher.assignment_for_student(@student).grade).to eq(95)
end
student_assignment_steps.rb
Given(/^I am a student$/) do
@student = Student.new #setting as an instance variable to access later on
@teacher = Teacher.new
end
When(/^I submit an assignment to my teacher$/) do
@assignment = Assignment.new
@teacher.submit_assignment(@student, @assignment)
end
Then(/^my teacher should have my assignment$/) do
expect(@teacher.assignment_for_student(@student)).to eq(@assignment)
end
teacher.rb
class Teacher
def initialize
@assignments = {}
end
def record_grade(student, grade)
assignment = @assignments[student] #assignment equal to assignments of the student
assignment.grade = grade
@assignments[student] = assignment
end
def submit_assignment(student, assignment)
@assignments[student] = assignment
end
def assignment_for_student(student)
@assignments[student]
end
end
teacher_spec.rb
require_relative "../lib/teacher"
require "rspec"
describe Teacher do
it "should store assignments" do
student = double
assignment = double
subject.submit_assignment(student, assignment)
expect(subject.assignment_for_student(student)).to eq(assignment)
end
describe "should record a grade" do
it "should record and the grade" do
student = double
assignment = double
expect(assignment).to receive(:grade=).with(95)
subject.submit_assignment(student, assignment)
subject.record_grade(student, 95)
end
end
end
assignment_spec.rb
require_relative "../lib/assignment"
describe Assignment do
it "should store a grade" do
subject.grade = 60
expect(subject.grade).to eq(60)
end
end
assignment.rb
class Assignment
attr_accessor :grade
end
student.rb
class Student
end
teacher_can_grade_assignment.feature
Feature: Teacher can grade assignment
As a Teacher
I can grade my students' assignments
So that they can know their knowledge level
Scenario: Teacher can grade assignment
Given I have a student
And They submitted an assignment
When They submitted an assignment
Then the assignment has a grade
您的测试告诉您 @teacher.assignment_for_student(@student).grade
是 nil
。不知何故,它没有正确设置。正如您所说,这出乎意料,您希望它是 95。
在 TDD 中,最好的下一步是找出失败测试中的更多状态。
添加一些额外的(临时的)期望以查看失败步骤中对象的状态:
expect(@teacher.assignment_for_student(@student)).to eq(@assignment)
expect(@teacher.class_variable_get(:@assigments).to include(@assignment)
即使您确定会失败的测试通常也会有很大帮助。
expect(@teacher.assignment_for_student(@student)).to be "failure"
通过在你的步骤周围散布这样的期望,你可以调试状态并查看代码集成错误的地方。
根据您粘贴的代码,我没有立即发现代码有任何问题。
您的作业没有评分,因为老师从未给它评分:您的功能没有调用 "I grade the assignment step"