Question

Can someone help me create mocks for the following code. I want to add a controller method in existing controller by following name and want to test its behavior to a movie class containing title, director, rating etc as table instances. Unfortunately I am unfamiliar with BDD commands to use here.

describe MoviesController do
  describe "#find_same_director" do
    before :each do
      fake_movies = [mock('movie1'), mock('movie2')]        
    end
    context "with invalid attributes" do 
      it "flashes no such director message" do
        flash[:notice].should_not be_nil
      end 
      it "redirects to the index method" do 
        response.should redirect_to movies_path
      end 
    end
    context "with valid attributes" do
      it "calls model method to find all movies" do 
        movie = Movie.find_with_director, {:director => 'George Lucas'}
        get :show, id: @fake_movies 
        assigns(:movie).should eq(@fake_results) 
      end 
      it "renders the #find_same_director view" do 
        get :find_same_director, id: @fake_movies
        response.should render_template :find_same_director 
      end 
    end
  end
end
Was it helpful?

Solution

Are you noticing that you are trying to test different things at different test cases? (the first contexts you aren't executing the action "get :x", the last ones you are doing "get :show"

First of all you should think about the behavior of your code, so, i can think of two contexts (what kind of situations do you have in this case):

  # with valid parameters(for e.g.: i should pass the right data, before this context i must create the data for the text).
  # with invalid parameters(for e.g: the parameters passed to the GET request should not be existent on the system).

Then you should think about what happens when this contexts are active.

  context "with valid parameters" do 
    it "should return the other movies of the same director, and assign it to the @movies"
    it "should render the template of the #find_same_director page"
  end 
  context "with invalid parameters" do
    it "should redirect to the movies_path"
    it "should put a flash message that the director is invalid"
  end

After you think about the test cases do you have to think about how to implement them, i'll give you a hint:

it "should return the other movies of the same director, and assign it to the @movies" do
  # THINKING ABOUT BDD HERE YOU SHOULD THINK OF THIS CODE SECTIONS AS FOLLOW:
  # GIVEN ( OR THE CONDITIONS FOR THE ACTION HAPPEN)
  @director = Director.new
  movies = [Movie.new, Movie.new]
  @director.movies = movies
  # HERE ILL FIX THE VALUES SO I CAN USE IT ON MY EXPECTATIONS
  Director.stub!(:find).with(@director_id).and_return(@director)
  # WHEN, THE ACTION HAPPENED
  get :find_same_director, :id => @director_id
  # THEN THE EXPECTATIONS THAT SHOULD BE MATCHED
  assigns(:movies).should == @director.movies
end

For a more real experience with tests i recommend you to watch the screencasts: http://destroyallsoftware.com/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top