Module Shoulda::InstanceMethods
In: lib/shoulda/context.rb
ValidationMatcher ValidatePresenceOfMatcher ValidateAcceptanceOfMatcher EnsureLengthOfMatcher ValidateFormatOfMatcher EnsureInclusionOfMatcher ValidateUniquenessOfMatcher ValidateNumericalityOfMatcher Context SetSessionMatcher RespondWithContentTypeMatcher SetTheFlashMatcher RenderWithLayout RespondWithMatcher AssignToMatcher FilterParamMatcher RouteMatcher HaveNamedScopeMatcher HaveDbIndexMatcher HaveDbColumnMatcher AllowMassAssignmentOfMatcher AllowValueMatcher AssociationMatcher HaveReadonlyAttributeMatcher lib/shoulda/context.rb lib/shoulda/action_controller/matchers/set_session_matcher.rb lib/shoulda/action_controller/matchers/respond_with_content_type_matcher.rb lib/shoulda/action_controller/matchers/set_the_flash_matcher.rb lib/shoulda/action_controller/matchers/render_with_layout_matcher.rb lib/shoulda/action_controller/matchers/respond_with_matcher.rb lib/shoulda/action_controller/matchers/assign_to_matcher.rb lib/shoulda/action_controller/matchers/filter_param_matcher.rb lib/shoulda/action_controller/matchers/route_matcher.rb Matchers Macros ActionController lib/shoulda/active_record/matchers/validation_matcher.rb lib/shoulda/active_record/matchers/have_named_scope_matcher.rb lib/shoulda/active_record/matchers/validate_numericality_of_matcher.rb lib/shoulda/active_record/matchers/have_db_index_matcher.rb lib/shoulda/active_record/matchers/ensure_inclusion_of_matcher.rb lib/shoulda/active_record/matchers/validate_format_of_matcher.rb lib/shoulda/active_record/matchers/validate_acceptance_of_matcher.rb lib/shoulda/active_record/matchers/have_db_column_matcher.rb lib/shoulda/active_record/matchers/allow_mass_assignment_of_matcher.rb lib/shoulda/active_record/matchers/allow_value_matcher.rb lib/shoulda/active_record/matchers/validate_presence_of_matcher.rb lib/shoulda/active_record/matchers/association_matcher.rb lib/shoulda/active_record/matchers/validate_uniqueness_of_matcher.rb lib/shoulda/active_record/matchers/have_readonly_attribute_matcher.rb lib/shoulda/active_record/matchers/ensure_length_of_matcher.rb Matchers Helpers Assertions Macros ActiveRecord ClassMethods Macros ActionView Private Helpers Assertions ActionMailer InstanceMethods Assertions Macros Shoulda dot/m_47_0.png

Methods

Public Instance methods

Returns an instance of the class under test.

  class UserTest
    should "be a user" do
      assert_kind_of User, subject # passes
    end
  end

The subject can be explicitly set using the subject class method:

  class UserTest
    subject { User.first }
    should "be an existing user" do
      assert !subject.new_record? # uses the first user
    end
  end

If an instance variable exists named after the described class, that instance variable will be used as the subject. This behavior is deprecated, and will be removed in a future version of Shoulda. The recommended approach for using a different subject is to use the subject class method.

  class UserTest
    should "be the existing user" do
      @user = User.new
      assert_equal @user, subject # passes
    end
  end

The subject is used by all macros that require an instance of the class being tested.

[Source]

     # File lib/shoulda/context.rb, line 232
232:     def subject
233:       @shoulda_subject ||= construct_subject
234:     end

Private Instance methods

[Source]

     # File lib/shoulda/context.rb, line 264
264:     def construct_subject
265:       if subject_block
266:         instance_eval(&subject_block)
267:       else
268:         get_instance_of(self.class.described_type)
269:       end
270:     end

[Validate]