I have the functionality of my code working correctly, but need to create an RSpec test that covers it. My code in movies_controller.rb:
def find_with_same_director
@movie = Movie.find(params[:id])
@movies, check_info = Movie.find_with_same_director(params[:id])
if check_info
flash[:notice] = "'#{@movie.title}' has no director info"
redirect_to movies_path
end
end
The find_with_same_director function in the model, movie.rb:
def self.find_with_same_director(id)
movie = self.find(id)
if !movie.director.blank?
movies = self.where(:director => movie.director).where.not(:id => movie.id)
return movies, false
else
return [], true
end
end
I am trying to write tests that covers the click on the "Find With Same Director" link that calls the function, when the movie that was clicked has a director to show, and when it doesn't. I've written the following tests in movies_controller_spec.rb for each so far:
describe 'find_with_same_director' do
it 'should call the find_with_same_director model method' do
expect(Movie).to receive(:find_with_same_director).with(params[:id])
get :find_with_same_director, id: movie.id
end
context 'movie has a director' do
let!(:movie1) {FactoryGirl.create(:movie, :director => movie.director)}
it do
get :find_with_same_director, id: movie1.id
expect(response).to redirect_to(movie_path(movie1.id))
end
end
context 'movie has no director' do
let!(:movie1) { FactoryGirl.create(:movie, :director => nil)}
it "should redirect to root" do
get :find_with_same_director, id: movie1.id
expect(response).to redirect_to(root_url)
end
end
end
I have spent hours working on these tests and while they are "covering" the lines when i check the report, they return fails. This means I wrote them incorrectly. I want to modify these tests to accurately represent what my controller code is doing and I would really appreciate some help with that. If you're feeling down to it I would also appreciate if you gave advice on writing the rspec test code for the model movie.rb file as well.
Aucun commentaire:
Enregistrer un commentaire