Ruby 如何进行绿色的Rspec测试?
下面有一段代码,作为一个参数,它从CSV文件的一行中获取信息,并创建一个Employee对象。我是Rspec的新手,虽然我知道我的解决方案按我希望的方式运行,但似乎无法通过测试。我希望在学习测试驱动开发的过程中更好地理解Rspec。我如何编写一个测试来证明这段代码是有效的?我意识到这个过程应该反过来进行(在创建最终产品之前先测试),但我这样做是为了尝试了解规范如何更好地工作。如果您还需要我的代码,请告诉我: CSV文件:Ruby 如何进行绿色的Rspec测试?,ruby,csv,rspec,tdd,automated-tests,Ruby,Csv,Rspec,Tdd,Automated Tests,下面有一段代码,作为一个参数,它从CSV文件的一行中获取信息,并创建一个Employee对象。我是Rspec的新手,虽然我知道我的解决方案按我希望的方式运行,但似乎无法通过测试。我希望在学习测试驱动开发的过程中更好地理解Rspec。我如何编写一个测试来证明这段代码是有效的?我意识到这个过程应该反过来进行(在创建最终产品之前先测试),但我这样做是为了尝试了解规范如何更好地工作。如果您还需要我的代码,请告诉我: CSV文件: first_name,last_name,annual_income,ta
first_name,last_name,annual_income,tax_paid,tax_rate
Johnny,Smith,120000,28000,38
Jane,Doe,140000,30000,40
Liz,Lemon,,21000,30
,Orsillio,40000,8800,18
Eric,Schmidt,54000,,28
员工类别:
class Employee
attr_reader :last_name, :first_name, :annual_income, :tax_paid, :tax_rate
def initialize(attributes) **#<-where the csv row is being passed in but in another main file used to run the code**
@last_name = attributes['last_name'] ||="[Last Name]"
@first_name = attributes['first_name'] ||="[First Name]"
@annual_income = attributes['annual_income'].to_f ||= 0
@tax_paid = attributes['tax_paid'].to_f ||= 0
@tax_rate = attributes['tax_rate'].to_f ||= 0
end
end
这是测试的结果输出:
F
Failures:
1) Employee instantiated Employee class object should instantiate when missing arguments are passed in
Failure/Error: expect(Employee.new("").first_name).to eql("[First Name]")
IndexError:
string not matched
# ./employee.rb:9:in `[]='
# ./employee.rb:9:in `initialize'
# ./employee_spec.rb:6:in `new'
# ./employee_spec.rb:6:in `block (2 levels) in <top (required)>'
Finished in 0.00029 seconds
1 example, 1 failure
Failed examples:
rspec ./employee_spec.rb:5 # Employee instantiated Em
F
失败:
1) 传入缺少的参数时,雇员实例化的雇员类对象应实例化
失败/错误:预期(Employee.new(“”.first_name).to eql(“[first name]”)
索引器:
字符串不匹配
#./employee.rb:9:in`[]='
#./employee.rb:9:in'initialize'
#/员工规格rb:6:in'new'
#./员工规范rb:6:in‘区块(2层)in’
以0.00029秒完成
1例,1例失败
失败的示例:
rspec./employee_spec.rb:5#employee实例化Em
这不是您的测试中的错误,而是您的测试很好地完成了它的工作,并告诉您代码有问题
你有
@last_name = attributes['last_name'] ||="[Last Name]"
在尝试分配属性参数之前,您可能应该检查属性参数是否包含您要查找的值?所有属性也是如此
此错误很可能是因为您将字符串作为参数而不是散列传入,但在调用Employee.new(“”
因此,将初始化方法更改为
def initialize(attributes) #**#<-where the csv row is being passed in but in another main file used to run the code**
if attributes.is_a? Hash
@last_name = attributes['last_name'] ||="[Last Name]"
@first_name = attributes['first_name'] ||="[First Name]"
@annual_income = attributes['annual_income'].to_f ||= 0
@tax_paid = attributes['tax_paid'].to_f ||= 0
@tax_rate = attributes['tax_rate'].to_f ||= 0
else
@last_name = "[Last Name]"
@first_name = "[First Name]"
@annual_income = 0
@tax_paid = 0
@tax_rate = 0
end
end
def initialize(attributes)#**#您能显示测试的结果输出吗?@jamesw我刚刚添加了测试的结果输出看起来您的测试工作得很好!我刚刚复制了您的类代码,并试图从rails控制台运行它,事实上,您的类中也存在相同的错误。所以你的问题应该是,“我怎样才能修好我的课?”我对这个想法一无所知,好吧,找出你的问题并添加了答案。请接受它,如果它有助于hanks,传递了一个空哈希而不是字符串,并且它可以工作
def initialize(attributes) #**#<-where the csv row is being passed in but in another main file used to run the code**
if attributes.is_a? Hash
@last_name = attributes['last_name'] ||="[Last Name]"
@first_name = attributes['first_name'] ||="[First Name]"
@annual_income = attributes['annual_income'].to_f ||= 0
@tax_paid = attributes['tax_paid'].to_f ||= 0
@tax_rate = attributes['tax_rate'].to_f ||= 0
else
@last_name = "[Last Name]"
@first_name = "[First Name]"
@annual_income = 0
@tax_paid = 0
@tax_rate = 0
end
end