这是我的rspec
文件:
require 'spec_helper'
describe Classroom, focus: true do
describe "associations" do
it { should belong_to(:user) }
end
describe "validations" do
it { should validate_presence_of(:user) }
end
describe "instance methods" do
describe "archive!" do
before(:each) do
@classroom = build_stubbed(:classroom)
end
context "when a classroom is active" do
it "should mark classroom as inactive" do
@classroom.archive!
@classroom.active.should_be == false
end
end
end
end
end
这是我的Classroom Factory
:
FactoryGirl.define do
factory :classroom do
name "Hello World"
active true
trait :archive do
active false
end
end
end
当上面的实例方法测试运行时,我收到以下错误:stubbed models are not allowed to access the database
我理解为什么会发生这种情况(但我缺乏测试知识/是测试新手),但不知道如何存根模型以使其不会影响数据库
工作 Rspec 测试:
require 'spec_helper'
describe Classroom, focus: true do
let(:classroom) { build(:classroom) }
describe "associations" do
it { should belong_to(:user) }
end
describe "validations" do
it { should validate_presence_of(:user) }
end
describe "instance methods" do
describe "archive!" do
context "when a classroom is active" do
it "should mark classroom as inactive" do
classroom.archive!
classroom.active == false
end
end
end
end
end