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

时间:2019-03-28 23:10:34

标签: ruby-on-rails ruby ruby-on-rails-3 rspec rspec-rails

我的代码功能正常运行,但是需要创建一个涵盖它的RSpec测试。 我的route.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

模型movie.rb中的find_with_same_director函数:

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>

我有点理解为什么会发生错误,但我不知道如何解决这些错误。

1 个答案:

答案 0 :(得分: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. FactoryGirl已弃用good 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!之间的区别。 / p>