类型错误:无法在“URL”上执行“createObjectURL”:未找到与提供的签名匹配的函数

2023-12-11

我有一个 Angular 8 应用程序,我用 jasmine karma 做了一些单元测试。这是 component.ts:

export class DossierPersonalDataComponent implements OnInit {
  dossier: DossierDto;
  editDossierForm: FormGroup;
  formBuilder = new FormBuilder();
  globalEditDossierErrors: ValidationErrors;
  dossierItems: DossierItemDto[] = [];
  profileImagefile: File;
  profileImageNeedsUpload = false;

  constructor(
    private dossierService: DossierService,
    private route: ActivatedRoute,
    private sanitizer: DomSanitizer,
    private dossierFileService: DossierFileService,
    private errorProcessor: ErrorProcessor,
    private dialog: MatDialog
  ) {
    this.dossierItems = this.route.snapshot.data.dossierItems;
    this.editDossierForm = this.formBuilder.group({});
    this.editDossierForm.disable();

    this.dossier = this.route.snapshot.data.dossier;
    this.dossierItems = route.snapshot.data.dossierItems;
    this.profileImagefile = this.route.snapshot.data.profileImage;

    this.editDossierForm = this.formBuilder.group({
      firstName: this.formBuilder.control(this.dossier.firstName, [Validators.required, Validators.maxLength(255)]),
      lastName: this.formBuilder.control(this.dossier.lastName, [Validators.required, Validators.maxLength(255)]),
      mobile: this.formBuilder.control(this.dossier.mobile, [Validators.maxLength(255)]),
      company: this.formBuilder.control(this.dossier.company, [Validators.maxLength(255)]),
      buddy: this.formBuilder.control(this.dossier.buddy, [Validators.maxLength(255)]),
      supervisor: this.formBuilder.control(this.dossier.supervisor, [Validators.maxLength(255)]),
      dateOfBirth: this.formBuilder.control(this.dossier.dateOfBirth)
    });
  }
  ngOnInit(): void {
    this.editDossierForm.disable();
  }

  editName() {
    this.editDossierForm.enable();
  }

  get profileImageUrl() {
    return this.profileImagefile === null
      ? '/assets/placeholder.jpg'
      : this.sanitizer.bypassSecurityTrustUrl(window.URL.createObjectURL(this.profileImagefile));
  }
}

这是规范文件:


describe('DossierPersonalDataComponent', () => {
  let component: DossierPersonalDataComponent;
  let fixture: ComponentFixture<DossierPersonalDataComponent>;

  beforeEach(async(() => {


    TestBed.configureTestingModule({
      imports: [HttpClientTestingModule, DossierModule, BrowserModule],
      declarations: [DossierPersonalDataComponent],
      providers: [
          DossierFileService,
          ErrorProcessor,
          {
            provide: ActivatedRoute,
            useValue: {
              snapshot: {
                data: {
                  dossier: {
                    firstName: 'hello',
                    lastName: 'world',
                    mobile: '111-111-1111',
                    company: 'carapax',
                    buddy: 'bud',
                    supervisor: 'super',
                    dateOfBirth: '1900-01-01',
                  },
                  dossierItems: [], // mock
                  profileImage: '',
                }
              }
            }
          },
        {
          provide: DomSanitizer,
          useValue: {
            sanitize: () => 'safeString',
            bypassSecurityTrustHtml: () => 'safeString'
          }
        }
      ]
    })
      .compileComponents()
      .then(() => {
        fixture = TestBed.createComponent(DossierPersonalDataComponent);
        component = fixture.componentInstance;
      });
  }));


  it('should create', () => {
    fixture.detectChanges();
    expect(component).toBeTruthy();
  }); 
});

但是当我运行测试时,我收到此错误:

DossierPersonalDataComponent > should create
TypeError: Failed to execute 'createObjectURL' on 'URL': No function was found that matched the signature provided.

在覆盖文件中,我将其视为黄色:

get profileImageUrl() {
    return this.profileImagefile === null
      ? '/assets/placeholder.jpg'
      : this.sanitizer.bypassSecurityTrustUrl(window.URL.createObjectURL(this.profileImagefile));
  }

所以这是黄色部分

? '/assets/placeholder.jpg'

分支未覆盖。

那么我必须改变什么?

谢谢

我正在使用谷歌浏览器

我改成这样:

provide: ActivatedRoute,
            useValue: {
              snapshot: {
                data: {
                  dossier: {
                    firstName: 'hello',
                    lastName: 'world',
                    mobile: '111-111-1111',
                    company: 'carapax',
                    buddy: 'bud',
                    supervisor: 'super',
                    dateOfBirth: '1900-01-01',
                  },
                  dossierItems: [], // mock
                  profileImagefile: null,
                }
              }
            }

但没有变化


似乎 karma chrome 不支持已弃用的createObjectURL。要使其与 karma 一起工作,您必须执行以下操作:

export class DossierPersonalDataComponent implements OnInit {
  dossier: DossierDto;
  editDossierForm: FormGroup;
  formBuilder = new FormBuilder();
  globalEditDossierErrors: ValidationErrors;
  dossierItems: DossierItemDto[] = [];
  profileImagefile: File;
  profileImageNeedsUpload = false;
  compWindow: any;

  constructor(
    private dossierService: DossierService,
    private route: ActivatedRoute,
    private sanitizer: DomSanitizer,
    private dossierFileService: DossierFileService,
    private errorProcessor: ErrorProcessor,
    private dialog: MatDialog
  ) {
    // this would help you to mock the "window" object in spec file
    this.compWindow = window;
    // please move below code to ngOnInit as standard practice of Angular.

    this.dossierItems = this.route.snapshot.data.dossierItems;
    this.editDossierForm = this.formBuilder.group({});
    this.editDossierForm.disable();

    this.dossier = this.route.snapshot.data.dossier;
    this.dossierItems = route.snapshot.data.dossierItems;
    this.profileImagefile = this.route.snapshot.data.profileImage;

    this.editDossierForm = this.formBuilder.group({
      firstName: this.formBuilder.control(this.dossier.firstName, [Validators.required, Validators.maxLength(255)]),
      lastName: this.formBuilder.control(this.dossier.lastName, [Validators.required, Validators.maxLength(255)]),
      mobile: this.formBuilder.control(this.dossier.mobile, [Validators.maxLength(255)]),
      company: this.formBuilder.control(this.dossier.company, [Validators.maxLength(255)]),
      buddy: this.formBuilder.control(this.dossier.buddy, [Validators.maxLength(255)]),
      supervisor: this.formBuilder.control(this.dossier.supervisor, [Validators.maxLength(255)]),
      dateOfBirth: this.formBuilder.control(this.dossier.dateOfBirth)
    });
  }
  ngOnInit(): void {
    this.editDossierForm.disable();
  }

  editName() {
    this.editDossierForm.enable();
  }

  get profileImageUrl() {
    return this.profileImagefile === null
      ? '/assets/placeholder.jpg'
      : this.sanitizer.bypassSecurityTrustUrl(this.compWindow.URL.createObjectURL(this.profileImagefile));
  }
}

In the spec.ts file:

describe('DossierPersonalDataComponent', () => {
  let component: DossierPersonalDataComponent;
  let fixture: ComponentFixture<DossierPersonalDataComponent>;
  const myWindow = {
    URL : {
      createObjectURL() { return 'something'; }
    }
  };

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      imports: [HttpClientTestingModule, DossierModule, BrowserModule],
      declarations: [DossierPersonalDataComponent],
      providers: [
          DossierFileService,
          ErrorProcessor,
          {
            provide: ActivatedRoute,
            useValue: {
              snapshot: {
                data: {
                  dossier: {
                    firstName: 'hello',
                    lastName: 'world',
                    mobile: '111-111-1111',
                    company: 'carapax',
                    buddy: 'bud',
                    supervisor: 'super',
                    dateOfBirth: '1900-01-01',
                  },
                  dossierItems: [], // mock
                  profileImage: '',
                }
              }
            }
          },
        {
          provide: DomSanitizer,
          useValue: {
            sanitize: () => 'safeString',
            bypassSecurityTrustHtml: () => 'safeString'
          }
        }
      ]
    })
      .compileComponents()
      .then(() => {
        fixture = TestBed.createComponent(DossierPersonalDataComponent);
        component = fixture.componentInstance;
        component.compWindow =  myWindow; // this would override the value we are setting in constructor. 
        fixture.detectChanges(); // once we have overridden it, now call "detectChanges"
      });
  }));


  it('should create', () => {
    expect(component).toBeTruthy();
  }); 
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

类型错误:无法在“URL”上执行“createObjectURL”:未找到与提供的签名匹配的函数 的相关文章

  • Jquery:表单验证不起作用

    我对 Jquery 很陌生 希望你们能帮助我解决这个 jquery 验证问题 一直在尝试验证表单 但它根本没有验证 它接受我在字段中输入的任何内容 无论我设置什么限制 请帮忙 谢谢 这是我的代码
  • Javascript - 删除粘贴上的空格

    我有一个最大长度为 10 的输入文本字段 该字段用于澳大利亚电话号码 10 位数字 电话号码通常分为以下语法 12 12345678 如果有人复制上面的内容并将其粘贴到我的输入字段中 显然会留下最后一位数字并保留空格 有没有办法在粘贴到输入
  • req.body 为空 Express js

    我花了几个小时试图找出为什么 req body 是空的 我在 stackoverflow 上到处查看并尝试了所有方法 但没有运气 Express js POST req body 空 https stackoverflow com ques
  • JavaScript - 如何通过键盘使用 onclick 函数

    我有 HTML 和 JavaScript 代码
  • 找出 Jquery ajax 请求被重定向到的位置

    所以 我收到了这个ajax请求 请参阅 金发女郎 大约6英尺高 看起来像这样 ajax url http example com makeThing dataType html type POST data something someot
  • 性能 - String.charAt(0) 与 /^.{1}/

    从概念上讲哪个应该更快 String charAt 0 or 1 regex String charAt 0 必须处理和应用正则表达式 速度测试资源 Paul S https stackoverflow com users 1615483
  • 仅在文件下载完成后设置 cookie。

    我有一个场景 我想告诉用户下载完成并提示关闭按钮 为此 我使用 jquery 插件来连续监视 cookie 以了解下载何时完成 我的问题是我想设置这个cookie fileDownload true and path 下载完成后立即进行 为
  • 通过修改 html 设置在 Web 表单上上传的默认文件名/目录

    我一直使用这个上传表单 并且每次都使用相同的文件名 我想知道是否有一种方法可以通过更改代码并在本地保存文件来设置表单中的文件名 如果有其他方法可以实现自动化 我也愿意 谢谢 这是来源
  • 从联合类型映射多个兼容类型

    我正在开发一个应用程序 我们已经定义了一个类型 并且需要从该单个接口中推导出多个接口 Example 我们的主要类型看起来像这样 该类型是否定义为映射类型或联合类型并不重要 因此最简单的解决方案都是最好的 type A type A inp
  • 删除添加空值的Javascript对象项[重复]

    这个问题在这里已经有答案了 我有一个 JavaScript 对象 finalTitleList Title ffd Iscompleted Id 0 Title fdfmdbk Iscompleted Id 1 Title fdf d Is
  • 如何检查 Node.js 中是否定义了变量?

    我正在用node js 编写一个程序 它实际上是js 我有一个变量 var query azure TableQuery 看起来这行代码有时没有执行 我的问题是 我怎样才能做到这样的条件 if this variable is define
  • 我想在数据表中使用 Div 结构而不是 Table。是否可以?

    我想用数据表 https datatables net 用div结构代替表格 目的是满足设计要求 有什么可能的方法或替代方案吗 不 您将无法执行此操作 Datatables 的核心仅适用于表格元素和子 thead tbody tfooter
  • fs.statSync 与缓冲区“错误:路径必须是没有空字节的字符串”

    我已经读入这样的文件缓冲区 let imageBuffer try imageBuffer fs readFileSync some path to image jpg catch e console log error reading i
  • 为什么闭包编译器不缩短这个?

    我不确定这只是一个错误还是一个预期的功能 基本上 我有这个微小的功能 我现在看到end这里是蓝色的 但这工作得很好 如果我将其重命名为其他名称 我仍然遇到问题 function f a b var start Math min a b va
  • Highcharts 异步服务器加载多个系列

    我正在尝试按照其示例使用 Highcharts 的延迟加载 http www highcharts com stock demo lazy loading http www highcharts com stock demo lazy lo
  • 如何在 JS 文件中使用 Github 机密

    我有一个基本的 git 存储库 其中包含用于构建和部署的 github 操作 主要是 HTML 和 TS 文件 但是我必须在一些需要保密的 API 密钥中使用 所以我想办法为他们使用 GITHUB SECRETS 如何在我的 js 或 TS
  • Angular 4 - “等待操作”的正确方法是什么?

    我遇到了一个简单的问题 有一个很奇怪的解决方案setTimeout 0 看看这个简单的代码 Component selector my app template div div
  • 如何使用 JavaScript 大致计算网站的连接速度?

    如何使用 JavaScript 大致计算网站的连接速度 我想创建一个像这样的javascript小部件 它将计算打开当前打开页面的速度 我想问是否可以仅使用 javascript 来完成此操作以及想法是什么 Update 请注意 页面大小始
  • 按 Chartjs 条形图的键对对象进行分组

    我正在尝试使用 Chart js 创建条形图 我在尝试根据每个用户的状态创建分组条形图时陷入困境 这是数据 statusId 0 firstName Joe status appealed count 1 statusId 0 firstN
  • 如何将react-native与php一起使用并获取返回数据始终为空

    我的回报始终为空 我似乎无法让它发挥作用 我如何将react native与php一起使用并获取json 任何人都可以帮忙吗 PHP myArray array myArray lat POST lat myArray lng POST l

随机推荐

  • 在android中动态添加布局到adapter的getview方法中

    我想在列表视图的每一行中显示 N 个图像视图 imageview的数量取决于json解析值 每次我从服务器获取 json 时 它可能是 2 3 或 4 所以我不能通过使用 inflate so 我决定在 getview 方法中创建动态视图并
  • 如果服务器上启用了内容安全策略,如何使用小书签将脚本注入页面?

    我有一个书签 它使用 jQuery 并解析页面上的一些元素 为了使用 jQuery 我动态创建一个脚本标签 使用 src 作为 jQuery URL 并附加到 head 标签 这对于许多网站都很有效 但是 像 Facebook 这样的网站很
  • 调用“ret”与调用 sys_exit 编号程序集 gcc 有什么区别

    在 gcc 汇编中 main 函数可以返回或退出 两者都起作用 这里我有两个程序 其中一个通过系统调用退出int 0x80 另一个简单地调用 ret 有什么不同 data hello string Hello World globl mai
  • 有什么方法可以在运行时调试电子表格应用程序脚本吗? [复制]

    这个问题在这里已经有答案了 有没有办法在运行时调试电子表格谷歌应用程序脚本 仅通过脚本编辑器运行它是没有用的 因为我需要调试的函数将触发事件作为参数 这是一个可以测试表单提交触发功能的函数 摘自如何测试 GAS 中的触发功能 functio
  • 尽管有标志,Chrome 并不将不安全的来源视为安全

    我想测试getUserMediaWindows 上 Chrome 上的不安全来源 我按照 goo gl rStTGz 上的说明以这种方式启动 Chrome 如果您使用不安全的来源 该链接会显示在控制台中getUserMedia start
  • 了解更多有关 JS 身高的快速资源

    JavaScript 中有很多与 高度 相关的属性 clientHeight Window height scrollHeight offsetHeight 等 我可以猜测他们是做什么的 但我想要一份正式的 详细的指南 通用谷歌搜索没有帮助
  • .net core类库调用.net Framework类库

    无法找到我的疑问的答案 希望有人能澄清 我创建了一个虚拟解决方案 1个类库 net框架 1 net核心库 试图参考任何一种方式 但我不能 它们不兼容 罚款是有道理的 现在我的问题 我有一个实用类库 net 框架 带有扩展 助手等 winfo
  • C# - 在背景图像上绘制顶部图像(alpha 通道) - WinForm

    我已经得到了这个带有 alpha 通道的顶部图像 我需要将此图像放在另一个背景图像上 而顶部图像的 alpha 通道显然保持完整 现在我已经看到了一些关于 Canvas 的教程 但我的项目似乎无法识别 Canvas 有人知道为什么我不能使用
  • 初始化条带变量后如何更改条带区域设置

    我正在使用条纹TypeScript应用程序 我读到可以使用如下语言环境设置 stripe 变量 var stripe Stripe pk test locale en 但如果我想在初始化后更改语言 我无法做到这一点 我想创建一个新的 str
  • 是否可以使用 SSH.NET 从单个登录会话执行多个 SSH 命令?

    我将 C 与 SSH NET 结合使用 并且能够使客户端 SSH 连接正常工作 以便通过 SSH 执行命令 我可以毫无问题地连接到虚拟机管理程序虚拟机上安装的 Linux 请读回StdOut and StdErr etc 然而 每个命令就像
  • 使用 Windows 服务进行屏幕截图

    即使有很多关于这个问题的问题 我也找不到合适的解决方案 我正在创建 Windows 服务来捕获屏幕 Windows 7 我尝试使用 Windows 应用程序 它工作正常 当我要启动该服务时 它说我无法启动该服务 当我检查 Windows 日
  • 单击图表时获取 X 轴值 - Excel VBA

    我遇到了一个奇怪的需求 当用户单击图表区域时 我需要从图表中获取 X 轴值 我知道我们可以为图表分配一个宏 这样 就可以创建图表的事件 但不知道如何进一步进行 请问有什么想法吗 Thanks 如果您的图表位于图表工作表中 则可以右键单击图表
  • “[变量] 在定义之前已被使用”错误

    我有几个这样的错误 我不知道如何 正确 解决它 问题是我有很多 javascript 文件 分开以便于维护 并且我包含插件等 所以在这个例子中我使用来自的快捷方式http www openjs com scripts events keyb
  • 删除缺失值超过阈值的行缺失值[关闭]

    Closed 这个问题是无法重现或由拼写错误引起 目前不接受答案 我有一个带有一些缺失值的矩阵 A lt array c 1 NA 3 NA 4 5 7 NA 2 dim c 3 3 我可以使用删除所有具有缺失值的行 B lt na omi
  • Applescript 使用特定库启动 iTunes

    我想编写一个 AppleScript 它允许我使用给定的库启动 iTunes 而不必按住 Option 键并浏览某个库 我已经知道道格的图书馆经理 但这并不是我想要的 AppleScript 将用于特定的库 iTunes 不允许您使用 Ap
  • 避免身份列中的空白

    我有一张桌子在MS SQL SERVER 2008我已经设置了它primary key自动递增 但如果我从此表中删除任何行并在表中插入一些新行 它将从下一个标识值开始 这会在标识值中创建间隙 我的程序要求所有身份或密钥按顺序排列 喜欢 分配
  • JavaScript 使用过滤器和循环从数组中删除多个值

    我是新来的 需要一些编写函数的帮助destroyer 从数组中删除多个值 destroyer 函数传入一个数组和附加数字作为参数 这个想法是从数组中删除数字 E g destroyer 1 2 3 1 2 3 2 3 Output 1 1
  • 如何转换Big Endian以及如何翻转最高位?

    我正在使用 TStream 读取二进制数据 感谢这篇文章 如何使用 TFileStream 将二维矩阵读入动态数组 我的下一个问题是数据是大端字节序 根据我的阅读 Swap 方法似乎已被弃用 我如何交换以下类型 16 bit two s c
  • 为ARM处理器编译基本C文件

    我正在使用 GCC 工具链的 Yagarto 重新编译 我正在尝试编译这个简单的程序以获得 elf可执行文件 int main void return 0 当输入命令时arm none eabi gcc main c我收到错误消息 c ya
  • 类型错误:无法在“URL”上执行“createObjectURL”:未找到与提供的签名匹配的函数

    我有一个 Angular 8 应用程序 我用 jasmine karma 做了一些单元测试 这是 component ts export class DossierPersonalDataComponent implements OnIni