我正在tutsplus上使用rspec的tdd教程运行bdd规范。本教程为变量分配了一个匿名函数,在“then”块中,它会在某个条件上引发错误。
以下是功能
Feature: Competition
As the boss
I want to manage competition
In order to improve my organization
Background: There is a team
Given There is a team called "Random"
Scenario: Team enters a competition without questions
Given I have a competition with no questions
When a team enters the competition
Then I should see an error
Scenario: Team enters a competition with questions
Given I have a competition with questions
When a team enters the competition
Then I should not see an error
以下是步骤定义代码
Given /There is a team called "([^"]*)"/ do |name|
@team = Team.new name
end
Given(/^I have a competition with( no)? questions$/) do |no_questions|
@competition = Competition.new
if no_questions
allow(@competition).to receive(:questions).and_return([])
else
allow(@competition).to receive(:questions).and_return([double])
end
end
When(/^a team enters the competition$/) do
@method = -> {@team.enter_competition @competition}
end
Then(/^I should( not)? see an error$/) do |dont_raise_error|
puts "dont_raise_error = #{dont_raise_error}"
if dont_raise_error
expect {@method}.not_to raise_error
else
expect(@method).to raise_error Competition::Closed
end
end
以下是团队类代码
class Team
def initialize(name)
end
def enter_competition competition
puts "In enter_competition"
raise Competition::Closed
end
end
比赛类代码
class Competition
class Closed < StandardError
end
end
规范在两个方案中都没有任何问题。但由于team.enter_competition总是抛出异常,因此当我遇到一些问题时,我期待它失败。我还想检查一下put_competition方法是否通过put来执行。但它永远不会在控制台上打印出来。它看起来永远不会被执行。
不确定这里发生了什么。如果有人能告诉我这里发生了什么,将会很有帮助。谢谢!