2013-03-29 3 views
0

난 그냥 루비 테스트 작업을 시작하고 테스트 에서처럼 코드를 작성하는 방법을 모르겠어요.루비 테스트 작업 방법?

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

그리고 터미널에서 레이크 명령에서 실행 :이 시도 내 코드 파일에서

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 

: 여기에 전체 테스트 파일에서 작업입니다. 레이크

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

어떤 책을 배우고 싶으십니까? –

+1

콘솔의 출력은 찾으려는 내용과 실제로 무엇을 발견했는지 알려주고 있어야합니다. 그것은 당신에게 잘못된 것에 대한 단서를 제공해야합니다. – depa

+0

좋습니다. 코드에서 "#ftoc"를 어떻게 기술해야합니까? –

답변

0

를 말한다보다 내가 대신 eq을 사용하여 문제

# 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 

나는 또한 ==를 사용하지 않는 당신을 추천하지 않고이 코드를 실행합니다. 예 : ftoc(32).should eq(0). 이 경우에도 차이는 없습니다. 난 그냥 업데이트 된 질문을보고

업데이트. 그래서 코드가 별도의 파일에 있습니까? Rspec은 귀하의 코드를 어떻게 알 수 있습니까? 코드가 표준 일치하는 레일스 파일 내에 있지 않으면 문제가됩니다.

경우에 따라 코드 파일을 사양으로 요구 한 다음 클래스의 새 인스턴스를 만들거나 (클래스 내에 메서드가있는 경우) 모듈을 사용하여 메서드를 전역에 노출해야합니다.

+0

안녕하세요. 이메일 상자에 테스트 파일을 보내 주시겠습니까? –

+0

@JohnOggy, 업데이트 된 답변을 확인하십시오. –

+0

감사합니다. 내 하찮은 영어 실력에 죄송하다는 말씀을 드리고 싶습니다. 나는 대답을 찾았다 –