首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >用Rspec3控制器进行belongs_to关联的instance_double测试

用Rspec3控制器进行belongs_to关联的instance_double测试
EN

Stack Overflow用户
提问于 2017-10-11 14:51:28
回答 2查看 310关注 0票数 0

我在测试和学习Rspec方面是新手,我不能让它发挥作用。

(我读过“使用Rspec3进行有效测试”一书,以及许多教程...also pluralsight.com)

情况很简单。在Companies控制器中,我想测试de方法,company模型belongs_to user,这是代码:

我认为问题是当执行

测试中:expect(Company).to receive(:new).with(company_params)

或在控制器中:@company.user=helpers.user

主计长:

代码语言:javascript
运行
复制
class CompaniesController < SessionsController

  def create
    @company=Company.new(company_params)     
    @company.user=helpers.user

    if @company.save()
      redirect_to companies_path
    else
      render :edit
    end
  end

和Rspec:

代码语言:javascript
运行
复制
RSpec.describe CompaniesController, type: :controller do

    let(:user) { instance_double(User) }

    before do
      allow_any_instance_of(ApplicationHelper).to receive(:user){user}
      allow(controller).to receive(:authorize){true}
    end

    describe 'Authenticated user with companies' do

      let(:company_params) { {company:{name:"Albert",domain:"www.albert.com"}} }
      let(:company) { instance_double(Company) }

      before do
        allow(Company).to receive(:new){company} 
      end

      describe 'POST #create' do
        context "with valid data" do

          before { allow(company).to receive(:save){true} }

          it "redirects to companies_path" do

            expect(Company).to receive(:new).with(company_params)
            expect(company).to receive(:user=).with(user)

            post :create, params:{company: company_params}
            expect(response).to redirect_to(companies_path)

          end

我的意图非常简单:使用instance_double模拟(或存根) @company和Company.new,使用实例double...to测试创建操作,并模拟返回true...etc的"save()“

我不知道我是否解释得很好,但考虑到控件的create操作,如何使用模拟存根instance_double?进行测试。

谢谢

EN

回答 2

Stack Overflow用户

发布于 2017-10-11 16:47:11

首先,让我解释一下我们需要在这里测试什么

代码语言:javascript
运行
复制
   def create
     @company=Company.new(company_params)     
     @company.user=helpers.user

     if @company.save()
       redirect_to companies_path
     else
       render :edit
     end 
   end

我们正在测试控制器的create动作。首先让我们看看这个动作是干什么的?它只是以comapany_params作为输入,并在数据库中创建公司记录。

测试也是一样的,我们只需要传递操作所需的输入,并检查它是否在数据库中创建记录。

代码语言:javascript
运行
复制
RSpec.describe CompaniesController, type: :controller do

    let(:user) { instance_double(User) }

   before do
    # all your authentication stubing goes here
    allow_any_instance_of(ApplicationHelper).to receive(:user){user}
    allow(controller).to receive(:authorize){true}
  end


  describe 'POST#create' do
    context 'with valid attributes' do
      before do
        post :create, { company:{ name:"Albert", domain:"www.albert.com"} }
      end

      it 'responds with success' do
        expect(response.status).to eq(302)
      end

      it 'creates company' do
        company = Company.find_by(name: "Albert")

        expect(assigns(:company)).to eq(company)
        expect(response).to redirect_to(companies_path())
      end
    end

    context 'with invalid attributes' do
      before do
        post :create, { company:{ name:"", domain:""} }
      end

      it 'renders new template' do
        expect(response).to render_template(:edit)
      end
    end
  end
end

没必要在这里潜入任何东西。据我所知,只有当我们在操作中使用任何lib classes / background jobs / third party libraries代码时,我们才需要对这些代码进行存根。因为对于所有这些,我们将分别编写规范。所以不需要在这里再测试,这就是为什么我们要做斯图比。

票数 1
EN

Stack Overflow用户

发布于 2017-10-16 15:32:12

多亏了Narsimha,我对如何测试有了更好的想法。即使,如果我想存根

代码语言:javascript
运行
复制
 @company=Company.new(company_params)     
 @company.user=helpers.user
 if @company.save()

对于只测试de create的响应,该解决方案很好地使用了参数,并允许allow(company).to receive(:user=)用于belongs_to关联。

代码语言:javascript
运行
复制
  let(:company_params) {{company:{name:"Albert",domain:"www.albert.com"}}}
  let(:ac_company_params) {ActionController::Parameters.new(company_params).require(:company).permit!}

  let(:company) { instance_double(Company) }

  before do
    allow(Company).to receive(:new){company} 
    allow(company).to receive(:user=)
    allow(company).to receive(:save){true}
  end

  it "redirects to companies_path" do
    expect(Company).to receive(:new).with(ac_company_params)
    expect(company).to receive(:user=).with(user)

    post :create, params: company_params
    expect(response).to redirect_to(companies_path)
  end
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46691366

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档