Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/reporting-services/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
模型的Rspec测试_Rspec_Ruby On Rails 4_Ruby On Rails 3.2 - Fatal编程技术网

模型的Rspec测试

模型的Rspec测试,rspec,ruby-on-rails-4,ruby-on-rails-3.2,Rspec,Ruby On Rails 4,Ruby On Rails 3.2,我有以下代码: def self.create_new_tenant(tenant_params, user_params, coupon_params) tenant = Tenant.new(:name => tenant_params[:name]) if new_signups_not_permitted?(coupon_params) raise ::Milia::Control::MaxTenantExceeded, "Sorry, new accounts n

我有以下代码:

def self.create_new_tenant(tenant_params, user_params, coupon_params)
  tenant = Tenant.new(:name => tenant_params[:name])
  if new_signups_not_permitted?(coupon_params)
    raise ::Milia::Control::MaxTenantExceeded, "Sorry, new accounts not permitted at this time" 
  else 
    tenant.save    # create the tenant
  end
  return tenant
end

在这两种情况下,如何编写条件为真和假的Rspec测试


首先,我想你可以把你的方法改成这样:

def self.create_new_tenant(tenant_params, coupon_params)
  unless new_signups_permitted?(coupon_params)
    raise ::Milia::Control::MaxTenantExceeded, "Sorry, new accounts not permitted at this time"
  end

  tenant = Tenant.new(:name => tenant_params[:name])
  tenant.save! # Use the "bang" method or check the return value
  tenant
end

def self.new_signups_permitted?(params)
  false
end
在这种情况下,您不需要显式地
返回
值,如果不允许新注册,也不需要初始化租户

我已将上一个方法的名称从
新注册\u不允许?
更改为
新注册\u允许?
,因为我认为它更清晰

顺便说一句,我删除了
用户参数
,因为您没有使用它

这是一个你需要的例子。可能不是这样的

describe ".create_new_tenant(tenant_params, coupon_params)" do
  context "when new signups are permitted" do
    before(:each) do
      allow(<Model>).to receive(:new_signups_permitted?) { true }
    end
    it "..." do
      expect { 
        <Model>.create_new_tenant(<tenant_params>, <coupon_params>)
      }.to change(Tenant, :count).from(0).to(1)
    end
  end
  context "when new signups aren't permitted" do
    before(:each) do
      allow(<Model>).to receive(:new_signups_permitted?) { false }
    end
    it "..." do
      expect { 
        <Model>.create_new_tenant(<tenant_params>, <coupon_params>)
      }.not_to change(Tenant, :count)
    end
    it "..." do
      expect { 
        <Model>.create_new_tenant(<tenant_params>, <coupon_params>)
      }.to raise_error "Sorry, new accounts not permitted at this time"
    end
  end
end
description.”创建新租户(租户参数、优惠券参数)“执行
上下文“当允许新注册时”执行
在…之前做
允许().接收(:新的\u注册\u允许?{true}
结束
它…“做什么
期待{
.创建新租户(,)
}。要更改(租户:计数)。从(0)。更改为(1)
结束
结束
上下文“当不允许新注册时”执行
在…之前做
允许().接收(:新注册允许?{false}
结束
它…“做什么
期待{
.创建新租户(,)
}.不可更改(租户:计数)
结束
它…“做什么
期待{
.创建新租户(,)
}。若要引发_错误“抱歉,此时不允许新帐户”
结束
结束
结束

您必须在
中定义所需的条件。允许新注册吗?
因为我想您不希望它总是返回
false

类似:预期异常为“抱歉,此时不允许新帐户”,但未出现任何问题。我已使用
上下文
块更新了答案修正错误。我已经检查了代码,它引发了错误,包括在rspec中。
describe ".create_new_tenant(tenant_params, coupon_params)" do
  context "when new signups are permitted" do
    before(:each) do
      allow(<Model>).to receive(:new_signups_permitted?) { true }
    end
    it "..." do
      expect { 
        <Model>.create_new_tenant(<tenant_params>, <coupon_params>)
      }.to change(Tenant, :count).from(0).to(1)
    end
  end
  context "when new signups aren't permitted" do
    before(:each) do
      allow(<Model>).to receive(:new_signups_permitted?) { false }
    end
    it "..." do
      expect { 
        <Model>.create_new_tenant(<tenant_params>, <coupon_params>)
      }.not_to change(Tenant, :count)
    end
    it "..." do
      expect { 
        <Model>.create_new_tenant(<tenant_params>, <coupon_params>)
      }.to raise_error "Sorry, new accounts not permitted at this time"
    end
  end
end