How to set private instance variable used within a method test?

rspec instance variable
rspec set instance variable
rspec mock instance variable

Given a class with a couple of instance variables and some methods. Some instance variables are set accessible via attr_reader and attr_accessor. Thus the others are private. Some of the private instance variables get set within one of the instance methods and read/utilized within another method.

For testing I'm using RSpec. As I'm still new to Ruby and want to get all things right, I defined my tests being rather fine-grained. Thus I've got one describe block for each instance method, which themselves are partitioned into a subset of contexts and its. General environmental preconditions are defined with before.

However, when testing one of the methods, which is utilizing but not setting one of the private variables, I need to call the other method, which is setting this variable. This seems rather overweight and not modular for me.

Is there a way of forcing a private instance variable to a certain value. Similar to "ripping out" the value of a private instance variable with Object::instance_eval(:var).

As you answered in your question the easiest way to set instance variable is with instance_eval method:

obj.instance_eval('@a = 1')

Another way is to use instance_variable_set:

obj.instance_variable_set(:@a, 1)

But I would not recommend to do this in your specs. Specs are all about testing behavior of an object and testing behaviour by breaking class encapsulation with instance_eval will make your tests more fragile and implementation dependent.

Alternative approach to object state isolation is to stub accessor methods:

class UnderTest
  attr_accessor :a

  def test_this
    do_something if a == 1

#in your test
under_test =

4. Methods Use Instance Variables: How Objects Behave, In other words, methods use instance variable values. to take an argument value and use it to set the value of an instance variable. in effect): mark your instance variables private and provide public getters and setters for access control. Well, if you ask how to do that on a discussion forum you’ll probably get the response that you shouldn’t test anything private. Instead, call the public method that’s calling using the instance variable and make sure it works the way it should. If the public method tests correctly then the private variable must be working.

Use instance_variable_set:

class SomeClass
  attr_reader :hello
  def initialize
    @hello = 5
  # ...

a =
a.hello    # => 5

a.instance_variable_set("@hello", 7)
a.hello    # => 7

Java - Variable Types, Each variable in Java has a specific type, which determines the size and can be stored within that memory; and the set of operations that can be applied to the variable. To declare more than one variable of the specified type, you can use a Instance variables are declared in a class, but outside a method, constructor � Instance variables are declared in a class, but outside a method, constructor or any block. When space is allocated for an object in the heap, a slot for each instance variable value is created. Instance variables are created when an object is created with the use of the keyword 'new' and destroyed when the object is destroyed.

I just solved it by creating a child and adding an accessor:

class HasSecrets

class TestMe < HasSecrets

def test_stuff
  pwned =
  pwned.muahaha = 'calc.exe'

9. Classes — Python 3.8.5 documentation, Class instances can also have methods (defined by its class) for modifying its state. are public (except see below Private Variables), and all member functions are virtual. As in Unlike C++ and Modula-3, built-in types can be used as base classes for In a sense the set of attributes of an object also form a namespace. To change the value of instance variable using method you need to use "setter" and "getter" method. Example : public class ABC { private String name; // instance variable // method to set the name in the object public void setName(String name) { = name; // store the name } // method to retrieve the name from the object public String getName() { return name; } public static void main

[PDF] Chapter 9: Classes with Instance Variables, a blueprint used to construct many objects. ○ a great way to partition a software system. ○ A way to implement any type. — A type defines a set of values, and the allowable operations on private class-name identifier ; all methods in the class can access instance variables use a unit test to test instances of the class . The instance variables are in the scope of an instance, so they cannot be accessed in a static method directly, but only with an instance reference qualifier: s._privateString in your case. However, this does not mean that instance variables are not visible for a static method inside the same class, as private means visible inside the class (for any member with any scope).

Language tour, Unlike Java, Dart doesn't have the keywords public , protected , and private . You can't use await or yield as an identifier in any function body marked with async Final instance variables must be initialized before the constructor body starts Where you declare the variable, set the value to a compile-time constant such� Thanks for the A2A, I really appreciate it. The best way I can think of to accomplish this is to instantiate the class in the static method and call the getter for the variable…

3.2 Instance Variables, set Methods and get Methods, Introduction to Classes, Objects, Methods and Strings in Java SE8 Class AccountTest is an application class in which the main method will create and use an private String name; // instance variable 8 9 // method to set the name in the 3.2.1 Account Class with an Instance Variable, a set Method and a� A-Since x is private, it cannot be accessed from an object foo. B-Since x is defined in the class Foo, it can be accessed by any method inside the class without using an object. You can write the code to access x without creating an object such as foo in this code. C-Since x is an instance variable, it cannot be directly used inside a main method.

  • Haven't thought about stubbing accessors yet. Would probably make a lot of my testing data (some text files, which should be detected and parsed by my program) obsolete. Seems a nice way. Thanks :)
  • Just to clarify, in your answer, are you claiming that instance_eval breaks class encapsulation or that instance_variable_set does as well?
  • @Seanny123 Both instance_eval and instance_variable_set modify internal state that is private to the instance and should not be modified outside of the object.
  • Isn't it also bad to create an accessor for the sole purpose of testing, since it gives the next person that reads your code the impression that the object's state will modified by an external object in the normal running of the program?
  • @Seanny123 Usually when you find yourself wanting to stub private variable then it is a sign that you need some kind of dependency injection (be it attr_accessor, constructor parameter or something else) and thus this state is part of the interface (meaning you just want to protect its modification by "mutator" method). Also sometimes it might be a smell in the design (but this is a broad topic and a little bit out of scope of this question).
  • Thank's for that. I knew it's easy. :)