I try to set a instance variable in a subject before testing validity of model fields. I need to set this variable, because validation is conditional (it is used only for some type of users). So I have something like this:
context "as a user" do
before(:each) do
subject = Organization.new
subject.editor = "user"
end
it { subject.should validate_presence_of :name }
end
But it doesn't work as expected:
Failure/Error: it { subject.should validate_presence_of :description }
RuntimeError:
Organization#editor attr is not set
What did i miss?
subject
in your before block is a local variable. It looks like you meant to use an explicit subject:
context "as a user" do
subject { Organization.new }
before(:each) do
subject.editor = "user"
end
# usually, you don't explicitly name the subject in an `it` like this
it { should validate_presence_of :name }
end