gpt4 book ai didi

ruby-on-rails - 使用 RSpec 测试 Rails Controller ——如何测试 : current_account. 项目

转载 作者:行者123 更新时间:2023-12-04 06:09:08 25 4
gpt4 key购买 nike

我正在使用 Rspec 和 Rails 3 进行测试。我已经测试了我的模型和助手,但我不知道如何开始测试 Controller 。我 Controller 操作中的几乎所有数据都是使用以下示例提取的:

@services = current_account.services

@projects = current_person.projects

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

我似乎找不到任何示例来说明如何测试以这种方式提取的数据。我发现的所有示例都不限于帐户或个人。谁能给我指点一篇关于如何模拟或 stub 这种类型安排的文章?这是否表明整个方法不正确?下面,我包含了一个完整的示例 Controller 。

如有任何帮助,我们将不胜感激。

谢谢,大卫

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

最佳答案

你不能用这样的东西来测试你的'index' Action 吗

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

如果我正确理解了你的问题,你应该能够 stub current_user.services(或项目)并让它返回一些已知值(在我的示例中由 FactoryGirl 生成)并检查它与存储在你的操作中的值(例如,在您的“索引”操作中使用@services)。

关于ruby-on-rails - 使用 RSpec 测试 Rails Controller ——如何测试 : current_account. 项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9877752/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com