LoginSignup
3

More than 5 years have passed since last update.

AngularJSで$sceを使ってるフィルタなんかのテスト

Last updated at Posted at 2014-09-16

$sceをDIしてるやつ、例えばこちらのようなののテストを書く場合。

newlines_spec.coffee
"use strict"

describe "Filter: newlines", ->

  beforeEach module "myApp"

  newlines = {}
  beforeEach inject ($filter) ->
    newlines = $filter "newlines"

  it "\nを<br />に変換して返すこと", ->
    text = "angularjs\nnewlines"
    expect(newlines(text)).toBe ("angularjs<br />newlines")

単純にこんな感じに書くと、下記のようなメッセージで失敗する

Expected { $$unwrapTrustedValue : Function } to be 'angularjs<br />newlines'.

ので、$$unwrapTrustedValueでexpectしてみた。

newlines_spec.coffee
  it "\nを<br />に変換して返すこと", ->
    text = "angularjs\nnewlines"
    expect(newlines(text).$$unwrapTrustedValue()).toBe ("angularjs<br />newlines")

テスト通ったけど、$sceProviderをテストの時は無効化するとか、他のやり方のほうがいいんだろうか。

sce_provider_desable.coffee
beforeEach module 'myApp',($sceProvider)->
  $sceProvider.enabled(false)
  return

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3