code: def test_evaluate_if_necessary(self):
      outdir = tempfile.mkdtemp()
  
      agent = mock.Mock()
      agent.act.return_value = "action"
      agent.get_statistics.return_value = []
  
      env = mock.Mock()
      env.reset.return_value = "obs"
      env.step.return_value = ("obs", 0, True, {})
  
      either_none = (self.n_steps is None) != (self.n_episodes is None)
      if not either_none:
          with self.assertRaises(AssertionError):
              agent_evaluator = evaluator.Evaluator(
                  agent=agent,
                  env=env,
                  n_steps=self.n_steps,
                  n_episodes=self.n_episodes,
                  eval_interval=3,
                  outdir=outdir,
                  max_episode_len=None,
                  step_offset=0,
                  save_best_so_far_agent=self.save_best_so_far_agent,
              )
      else:
          value = self.n_steps or self.n_episodes
          agent_evaluator = evaluator.Evaluator(
              agent=agent,
              env=env,
              n_steps=self.n_steps,
              n_episodes=self.n_episodes,
              eval_interval=3,
              outdir=outdir,
              max_episode_len=None,
              step_offset=0,
              save_best_so_far_agent=self.save_best_so_far_agent,
          )
  
          agent_evaluator.evaluate_if_necessary(t=1, episodes=1)
          self.assertEqual(agent.act.call_count, 0)
  
          agent_evaluator.evaluate_if_necessary(t=2, episodes=2)
          self.assertEqual(agent.act.call_count, 0)
  
          # First evaluation
          agent_evaluator.evaluate_if_necessary(t=3, episodes=3)
          self.assertEqual(agent.act.call_count, value)
          self.assertEqual(agent.stop_episode.call_count, value)
          if self.save_best_so_far_agent:
              self.assertEqual(agent.save.call_count, 1)
          else:
              self.assertEqual(agent.save.call_count, 0)
  
          # Second evaluation with the same score
          agent_evaluator.evaluate_if_necessary(t=6, episodes=6)
          self.assertEqual(agent.act.call_count, 2 * value)
          self.assertEqual(agent.stop_episode.call_count, 2 * value)
          if self.save_best_so_far_agent:
              self.assertEqual(agent.save.call_count, 1)
          else:
              self.assertEqual(agent.save.call_count, 0)
  
          # Third evaluation with a better score
          env.step.return_value = ("obs", 1, True, {})
          agent_evaluator.evaluate_if_necessary(t=9, episodes=9)
          self.assertEqual(agent.act.call_count, 3 * value)
          self.assertEqual(agent.stop_episode.call_count, 3 * value)
          if self.save_best_so_far_agent:
              self.assertEqual(agent.save.call_count, 2)
          else:
              self.assertEqual(agent.save.call_count, 0)
  
imports: import tempfile
  import unittest
  from unittest import mock
  from chainer import testing
  import chainerrl
  from chainerrl.experiments import evaluator
label_desc: Use image node_id 1 for calling the TestEvaluator obj's underlying member method code with example usage: obj.test_evaluate_if_necessary() without return types