お知らせ

現在サイトのリニューアル作業中のため、全体的にページの表示が乱れています。
投稿日:
言語::TypeScript開発::テストNode.js::Jest

単体テストの観点でいうとprocess.argvそのものはモックしないほうが良いと思っているので、本記事ではprocess.argv自体をモックする方法と、それを回避する方法を紹介する

サンプルコード

正攻法

describe('example', () => {
  afterEach(() => {
    // 元に戻しておく
    process.argv.length = 2;
  });
  it('got first param', () => {
    process.argv.push('param1');
    expect(process.argv.length).toBe(3);
    expect(process.argv[2]).toBe('param1');
  });
});

そもそもモックせずに済むように設計する

process.argvを利用している関数でprocess.argvを引数に取ればモック不要となる

const getArgv = (argv: string[]) => {
    // some procedure
}

describe('getArgv', () => {
  it('got first param', () => {
    const argv = [,, 'param1'];
    expect(argv.length).toBe(3);
    expect(argv[2]).toBe('param1');
  });
});
投稿日:
言語::JavaScript言語::HTML

JavaScriptで添付ファイルを拾うのと、ファイルを落とす処理のサンプルコード

<html>
<head>
  <meta charset='utf-8'>
  <title>js file up/dl example</title>
  <script>
    window.onload = () => {
      const fr = new FileReader();
      const el = document.getElementById("test");

      fr.onload = (ev) => {
        const dl = document.createElement('a');
        dl.setAttribute('href', ev.target.result);
        dl.setAttribute('download', el.files[0].name);
        dl.style.display = 'none';
        document.body.appendChild(dl);
        dl.click();
        document.body.removeChild(dl);
      };

      el.onchange = () => {
        fr.readAsDataURL(el.files[0]);
      };
    };
  </script>
</head>
<body>
  <input type="file" id="test"></input>
</body>
</html>
投稿日:
OS::Windowsミドルウェア::HTTPD::nginx言語::PHP

確認環境

Env Ver
Windows 10 pro
PHP 8.0.2 NTS Visual C++ 2019 x64
nginx 1.19.8

手順

  1. nginx.confを開きFastCGI server listening on 127.0.0.1:9000辺りに次の設定をする
location ~ \.php$ {
    root           html;
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  C:/nginx/html/$fastcgi_script_name;
    include        fastcgi_params;
}
  1. 設定したフォルダに.phpファイルを配置
  2. nginxを起動
  3. php-cgi.exe -b 127.0.0.1:9000
  4. .phpファイルにアクセス

サービス化する

  • winswを使うとできるらしい

フックを単体でテストするケースを想定。

このパターンはコンポーネントからフックを切り離しているケースで有用。手法としては@testing-library/react-hooksrenderHook()を使う。

カスタムフック

export const useUserForm = () => {
  const [username, setUsername] = useState('');
  const [password, setPassword] = useState('');

  const onChangeUserName = (ev: string) => {
    setUsername(ev);
  };
  const onChangePassword = (ev: string) => {
    setPassword(ev);
  };

  return {
    username,
    password,
    onChangeUserName,
    onChangePassword,
  };
};

テストコード

it('onChangeUserName で username が設定されること', () => {
  // `renderHook` で Hook をレンダリング
  const { result } = renderHook(() => useUserForm());
  // `act()` で Hook のイベントを叩く
  act(() => result.current.onChangeUserName('foo'));
  // 結果を見る
  expect(result.current.username).toBe('foo');
});
投稿日:
言語::C#.NET
  1. System.ReactiveをNuGetから入れる
  2. this.ticker= Observable.Interval(new TimeSpan(0, 0, 15));でObserverを作る
this.ticker.Subscribe(
    (item) => {
        // 任意のコントロールを操作する例
        // this.Invokeでコントロールを操作するメソッドを呼び出すと実現できる
        this.Invoke(
            (MethodInvoker)(delegate () {
                // ここに処理
            })
        );
    }
);

どうでもいいけどRx.NETってちゃんとしたリファレンスなくね?