简体   繁体   English

如何为以下代码创建RSpec测试

[英]How to create RSpec tests for the following code

I need to create RSpec testing for the following ruby code and seem to run into issues every time I try. 我需要为以下ruby代码创建RSpec测试,并且每次尝试时都会遇到问题。 I would love an example or two of RSpec tests that could be created for the following code/methods which are in my controller: 我希望能够为我控制器中的以下代码/方法创建一个或两个RSpec测试:

  def edit
    @movie = Movie.find params[:id]
  end
   def update
    @movie = Movie.find params[:id]
    @movie.update_attributes!(movie_params)
    flash[:notice] = "#{@movie.title} was successfully updated."
    redirect_to movie_path(@movie)
  end
   def destroy
    @movie = Movie.find(params[:id])
    @movie.destroy
    flash[:notice] = "Movie '#{@movie.title}' deleted."
    redirect_to movies_path
  end
   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

I have this so far: 到目前为止我有这个:

RSpec.describe MoviesController, type: :controller do
  it 'should get all movies in the database' do
      get :index
      expect(response).to render_template :index
      expect(assigns[:movies]).to eq(Movie.all)
  end
  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(no_args)
      get :find_with_same_director, id: movie.id
    end
  end
end

but it is not covering it correctly. 但它没有正确覆盖它。 Any help would be appreciated, thanks. 任何帮助将不胜感激,谢谢。

RSpec.describe MoviesController, type: :controller do
 it 'should get all movies in the database' do
    get :index
    expect(response).to render_template :index
    expect(assigns[:movies]).to eq(Movie.all)
 end

 it 'should update movie in the database' do
   Factory.girl.create(:movie)
   before_movie_count = Movie.count

   put :update, id: movie.id

   expect(assigns[:movies]).to eq(...) # whatever data is passed

   expect(Movie.count).to eq(before_ml_count)
   expect(flash[:snack_class]).to eq(:notice.to_s)
   expect(flash[:snack_message]).to ends_with('was successfully updated')
   expect(response).to have_http_status(:redirect)
 end

 it 'should delete movie in the database' do
    Factory.girl.create(:movie)
    before_movie_count = Movie.count

    delete :update, id: movie.id

    expect(Movie.count).to eq(before_ml_count - 1)
    expect(flash[:snack_class]).to eq(:notice.to_s)
    expect(flash[:snack_message]).to ends_with('deleted')
    expect(response).to have_http_status(:redirect)
 end

 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(no_args)
     get :find_with_same_director, id: movie.id
   end
 end
end


#### in factory movie.rb

FactoryGirl.define do
  factory :movie do
    # assign data to movie attributes here.
  end
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM