2012-08-02 7 views
1

나는 기본적인 의문이있다. rspec의 컨텍스트

describe Name do 
context "number1" do 
....... 
....... 
end 
context "number 2" do 
....... 
....... 
end 
context "number 3" do 
....... 
....... 
end 

어떻게 상황의 각각의 기능은 .rb 파일에 설명해야합니다 은 RSpec에 파일이 많은 컨텍스트가 포함 된 경우? 그들은 같은 반 또는 다른 반에 있어야 하는가? 이것에 대한 지식을 향상시키기 위해 읽을 수있는 책이 있습니까?

답변

8

rspec 파일을 정의 할 때 사용하는 구조 (rspec에서 수행 한 읽기 기준)는 describes을 사용하여 특정 기능을 설명하고 context은 상태 및/또는 경로의 특정 컨텍스트를 함수를 통해 설명한다는 것입니다 .

예 클래스 : 당신이 볼 수 있듯이, 내가 클래스 메소드 정말 바보 랜덤 기능을 인스턴스 메서드를 정의한

class MyClass 
    def self.my_class_method(bool) 
     if bool == true 
      return "Yes" 
     else 
      return "No" 
     end 
    end 

    def my_instance_method 
     today = Date.today 
     if today.month == 2 and today.day == 14 
      puts "Valentine's Day" 
     else 
      puts "Other" 
     end 
    end 
end 

. 그러나 요점은 이것입니다 : 클래스 메서드는 인수를 기반으로 뭔가 다른 작업을 수행하고 인스턴스 메서드는 일부 외부 요소에 따라 다른 작업을 수행합니다.이 모든 것을 테스트해야하며 이들은 컨텍스트과 다릅니다. 그러나 의 rspec 파일에있는 기능을 설명합니다.

RSpec에 파일 :

describe MyClass do 
    describe ".my_class_method" do 
     context "with a 'true' argument" do 
      it "returns 'Yes'." do 
       MyClass.my_class_method(true).should eq "Yes" 
      end 
     end 

     context "with a 'false' argument" do 
      it "returns 'No'." do 
       MyClass.my_class_method(false).should eq "No" 
      end 
     end 
    end 

    describe "#my_instance_method" do 
     context "on Feb 14" do 
      it "returns 'Valentine's Day'." do 
       Date.stub(:today) { Date.new(2012,2,14) } 
       MyClass.new.my_instance_method.should eq "Valentine's Day" 
      end 
     end 

     context "on a day that isn't Feb 14" do 
      it "returns 'Other'." do 
       Date.stub(:today) { Date.new(2012,2,15) } 
       MyClass.new.my_instance_method.should eq "Other" 
      end 
     end 
    end 
end 

그래서 당신은 describe을 볼 수 있습니다 당신이 설명하는지 방법을 말씀입니다, 그리고 클래스의 메소드의 이름까지 일치합니다. context은 메소드가 호출 될 수있는 여러 조건 또는 메소드 작동 방식에 영향을주는 여러 상태를 평가하는 데 사용됩니다.

희망이 도움이됩니다.

+0

저에게 명확한 이해를 주셔서 감사합니다. – user1568617

+0

여러분을 환영합니다! – MrDanA

+0

+1 이것은 정확하게 내가하는 방식입니다. – severin

관련 문제