例如,这两个测试:RSpec中'expect'和'should'是否有区别?
it "fails to create given a bad player list" do
Team.new("Random name", bad_players).should raise_error
end
和
it "fails to create given a bad player list" do
expect {Team.new("Random name", bad_players)}.to raise_error
end
它们返回不同的输出(第一个出现故障,而第二传递一个)。根据团队模型的内容,我希望既可以通过也可以失败。
完整RSpec的代码是:
require_relative 'team'
describe "Team" do
it "has a name" do
Team.new("Random name").should respond_to(:name)
end
it "has a list of players" do
Team.new("Random name").players.should be_kind_of Array
end
it "is favored if it has a celebrity on it"
it "complains if there is a bad word in the name"
context "given a bad list of players" do
let(:bad_players) { {} }
it "fails to create given a bad player list" do
expect { Team.new("Random name", bad_players) }.to raise_error
end
end
end
我想原因“应该”会工作是因为在类似的语法的“它‘有一个名为’”测试。如果事实证明'应该'是不正确的,'期望...到'是正确的,我很想知道什么时候使用一个和另一个。谢谢。