如何为以下代码编写RSpec测试

问题描述 投票:-1回答:1

我的代码功能正常,但需要创建一个覆盖它的RSpec测试。我的routes.rb:

  resources :movies do
    #member routes for individual ones
    get 'find_with_same_director', on: :member
  end
  # map '/' to be a redirect to '/movies'
  root :to => 'movies#index'

我在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

模型中的find_with_same_director函数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

我正在尝试编写测试,其中包含单击“使用相同控制器查找”链接以调用该函数,当单击的电影有导演要显示时,以及何时不显示。到目前为止,我已经在movies_controller_spec.rb中编写了以下测试:

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
    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(/movies)
    end
  end
end

我花了几个小时研究这些测试,当我检查报告时他们“覆盖”这些行,前两次返回失败。这意味着我写错了。我想修改这些测试以准确地表示我的控制器代码正在做什么,我真的很感激一些帮助。如果您对此感到满意,我也非常感谢您提供有关为模型movie.rb文件编写rspec测试代码的建议。

我隔离第一个测试时得到的错误:

  1) MoviesController find_with_same_director should call the find_with_same_director model method
     Failure/Error: expect(Movie).to receive(:find_with_same_director).with(params[:id])

     NameError:
       undefined local variable or method `params' for #<RSpec::ExampleGroups::MoviesController::FindWithSameDirector:0x000000056b27e0>

我隔离第二个测试时得到的错误:

Failures:

  1) MoviesController find_with_same_director movie has a director should redirect to "/movies/28"
     Failure/Error: expect(response).to redirect_to(movie_path(movie2.id))
       Expected response to be a <redirect>, but was <200>

我有点理解错误发生的原因,我只是不知道修复它们。

ruby-on-rails ruby ruby-on-rails-3 rspec rspec-rails
1个回答
1
投票

测试通常是孤立的。因此,让我们一个接一个地看着它们:

你的第一个规范是这样的:

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

重要的是要注意,在此测试的上下文中,paramsmovie确实存在,因为您确实首先定义它们。您可能希望通过首先创建电影来解决此问题:

let(:movie) { FactoryGirl.create(:movie) }

it 'should call the find_with_same_director model method' do
  expect(Movie).to receive(:find_with_same_director).with(movie.id)
  get :find_with_same_director, id: movie.id
end

两个建议:

  1. FactoryGirlgood reason而被弃用。它被FactoryBot取代。请更新它。
  2. IMO这个规范根本不存在,因为它测试了一个内部实现细节。测试应具体说明方法返回的内容或方法的副作用。但测试不应该测试某些事情是如何完成的。原因很简单。当您重构该方法时,即使该方法仍返回精确的似乎响应,此类测试也会中断。

你的第二个规范:

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(:movie) { FactoryGirl.create(:movie, :director => nil) } # <= this creates the movie for the test

  it "should redirect to root" do
    get :find_with_same_director, id: movie.id
    expect(response).to redirect_to(/movies)
  end
end

此外,我建议用范围替换find_with_same_director并查看Rspec的文档 - 特别是控制器规范如何工作以及letlet!之间的区别。

© www.soinside.com 2019 - 2024. All rights reserved.