2013-03-29 88 views
0

我刚开始使用ruby测试,并不知道如何在测试中编写代码。 这里是测试文件的完整任务:如何使用Ruby测试?

require "temperature" 

describe "temperature conversion functions" do 
    describe "#ftoc" do 
    it "converts freezing temperature" do 
     ftoc(32).should == 0 
    end 

    it "converts boiling temperature" do 
     ftoc(212).should == 100 
    end 

    it "converts body temperature" do 
     ftoc(98.6).should == 37 
    end 

    it "converts arbitrary temperature" do 
     ftoc(68).should == 20 
    end 
    end 

    describe "#ctof" do 
    it "converts freezing temperature" do 
    ctof(0).should == 32 
    end 

    it "converts boiling temperature" do 
    ctof(100).should == 212 
    end 

    it "converts arbitrary temperature" do 
    ctof(20).should == 68 
    end 
    end 
end 

在我的代码文件,我试试这个:

def ftoc(f) 
    (f - 32)/1.8 
end 

而且从rake命令从终端运行它。比耙说

temperature conversion functions 
#ftoc 
converts freezing temperature 
converts boiling temperature 
converts body temperature (FAILED - 1) 
+0

你指的是哪本书学习这个? –

+1

控制台中的输出应该告诉你它预期会发现什么以及它实际发现了什么。这应该会给你提供进一步的线索。 – depa

+0

好吧,我应该如何在代码中描述“#ftoc”? –

回答

0

我运行这段代码没有任何问题

# controllers/temp_spec.rb 
require 'spec_helper' 

describe "#ftoc" do 
    it "converts freezing temperature" do 
    ftoc(32).should == 0 
    end 
end 

def ftoc(f) 
    (f - 32)/1.8 
end 

# $ rspec spec/controllers/temp_spec.rb 
# => One example, 0 failure 

我也建议你不要使用==,而不是使用eq。例如ftoc(32).should eq(0)。虽然在这种情况下没有任何区别。

更新

我刚才看到你更新的问题。所以你的代码是在单独的文件? Rspec如何知道你的代码?如果你的代码不在标准的Rails文件中,那就是问题所在。

在你的情况下,你需要在规范中要求代码文件,然后创建类的新实例(如果方法在类中),或者使用模块将方法暴露给全局。

+0

你好比尔。我可以将测试文件发送到您的电子邮箱吗? –

+0

@JohnOggy,检查我的更新答案。 –

+0

谢谢大家的帮助。对不起,我的英语不好。我找到答案 –

相关问题