Тестирование контроллеров Rails с помощью RSpec – How to test: current_account.projects - PullRequest
3 голосов
/ 26 марта 2012

Я использую Rspec и Rails 3 для тестирования.Я проверил свои модели и помощников, но забыл о том, как начать тестирование контроллеров.Почти все мои данные в действиях моего контроллера извлекаются, используя что-то вроде этих примеров:

@services = current_account.services

@projects = current_person.projects

@projects = current_account.projects.active 
# this is really @projects = current_person.account.projects.active)

Я не могу найти никаких примеров того, как тестировать данные, которые извлекаются таким образом.Все примеры, которые я нашел, не относятся к аккаунту или человеку.Может кто-нибудь указать мне статью о том, как издеваться или заглушить этот тип договоренности?Является ли это признаком того, что весь этот подход не верен?Ниже я включил весь пример контроллера.

Любая помощь будет принята с благодарностью.

Спасибо, Дэвид

class ServicesController < ApplicationController
  # Run authorizations
  filter_resource_access

  # Respond to ...
  respond_to :html, :xml, :json
  respond_to :js,   :only => [:new, :create, :edit, :update, :destroy]

  # GET /services
  # GET /services.xml
  def index
    @services = current_account.services.order("name").paginate(:page => params[:page])

    respond_with(@services)
  end

  # GET /services/1
  # GET /services/1.xml
  def show
    @service = current_account.services.find(params[:id])

    respond_with(@service)
  end

  # GET /services/new
  # GET /services/new.xml
  def new
    @service = current_account.services.new

    respond_with(@service)
  end

  # GET /services/1/edit
  def edit
    @service = current_account.services.find(params[:id])

    respond_with(@service)
  end

  # POST /services
  # POST /services.xml
  def create
    @service = current_account.services.new(params[:service])

    if @service.save
      # flash[:notice] = 'A service was successfully created.'
    end

    respond_with(@service, :location => services_url)
  end

  # PUT /services/1
  # PUT /services/1.xml
  def update
    @service = current_account.services.find(params[:id])

    if @service.update_attributes(params[:service])
      # flash[:notice] = 'The service was successfully updated.'
    end

    respond_with(@service, :location => services_url)
  end

  # DELETE /services/1
  # DELETE /services/1.xml
  def destroy
    @service = current_account.services.find(params[:id])

    if @service.destroy
      flash[:notice]  = "The service was successfully deleted."
    else
      flash[:warning] = @service.errors.full_messages.inject("") { |acc, message| acc += message  }
    end

    respond_with(@service)
  end
end

–––––– ОБНОВЛЕНИЕ

Благодаря решению Xaid я смог получить решение:

  context "logged_in" do
    before(:each) do
      @current_account = Factory.create(:account)
      controller.stub!(:current_account).and_return(@current_account)

      @services = FactoryGirl.create_list(:service, 10, :account => @current_account)
      @services << @current_account.services.first
      @current_account.services.stub!(:all).and_return(@services)
    end


    # INDEX
    describe "GET services" do
      before(:each) do
        get :index
      end

      it "should set @services when accessing GET /index" do
        assigns[:services].should == @services
      end

      it "should respond with success" do
        response.should be_success
      end
    end
  end

1 Ответ

4 голосов
/ 27 марта 2012

Разве вы не можете использовать что-то подобное для проверки действия 'index'

describe "GET 'index'" do
  before(:each) do
    @user = FactoryGirl.create(:user)
    controller.stub!(:current_user).and_return(@user)
    @services = FactoryGirl.create_list(:service, 10, :user => @user)
    @user.services.stub!(:all).and_return(@services)
  end

  it "should return a list of services" do
    get :index
    assigns(:services).should == @services
  end
end

Если я правильно понял ваш вопрос, вы должны иметь возможность заблокировать current_user.services (или проекты) и сделать еговерните некоторое известное значение (сгенерированное FactoryGirl в моем примере) и сравните его со значением, хранящимся в вашем действии (например, @services в вашем действии index).

...