Thursday, February 17, 2011

Should A Custom Method Be Used In The before(:each) Section

I have a method on the model Category called create_main used to create main categories. Should I use this method in the before(:each) section even though the method itself has to be tested, or should the main category be created manually using rails built in functionality.

From stackoverflow
  • It should be possible to partition your Examples into two Example Groups, one group where before(:each) is called with create_main, and you use this to test everything except create_main. Then, you have another subset, where before(:each) does not call create_main, and here you test create_main.

    In your case, I think you could try something like the following:

    describe Category, " without a main category" do
      before(:each) do
        # No call to create_main here
      end
    
      it "should create the main category" do
        # Here we test that create_main is working
      end
    end
    
    describe Category, " with a main category already created" do
      before(:each) do
        # This time, we do call create_main to set up the object as necessary
      end
    
      # More examples go here that depend on create_main
    end
    

    Give that a shot. I'm not 100% sure it works, but I've seen similar setups in the past.

0 comments:

Post a Comment