【发布时间】:2014-06-13 23:32:15
【问题描述】:
我有一个 Account 模型来验证其子域是唯一的。
我正在尝试学习如何使用 RSpec 测试控制器。
这是我想出的,但它与生成的 RSpec 测试完全不同,我想知道这是否是测试它的好方法,或者是否有更好的方法。
我的测试:
describe "POST create" do
describe "with valid params" do
it "creates a new Account" do
original_count = Account.count
account = FactoryGirl.build(:account, :subdomain => 'newdomain')
post :create, {:account => account}
account.save!
new_count = Account.count
expect(new_count).to eq(original_count + 1)
end
...
编辑
我忘了指出,在我的 spec_helper 中有以下代码。由于我处理子域的方式需要它:
config.before(:each, :type => :controller) do
@account = FactoryGirl.create(:account)
@user = FactoryGirl.create(:user)
@request.host = "#{@account.subdomain}.example.com"
sign_in @user
end
【问题讨论】:
-
我要么还没有让这些工作,要么还没有回到这个。
标签: ruby-on-rails ruby rspec factory-bot rspec-rails