我有很多几乎相同的测试。为了 DRY 和可扫描性,我想将测试抽象为一个函数,然后使用一些参数调用该函数。然后该函数将调用 它 并将规范添加到套件中。
它似乎可以工作,除了规范不会以与其他规范相同的方式运行,并且 beforeEach 不会在公共(public)函数中定义的规范之前被调用。
define(['modules/MyModule','jasmine/jasmine'], function(MyModule) {
describe('myModule', function() {
function commonTests(params) {
it('should pass this test OK', function() {
expect(true).toBe(true);
});
it('should fail because module is undefined', function() {
expect(module[params.method]()).toBe('whatever');
});
}
var module;
beforeEach(function() {
module = new MyModule();
});
describe('#function1', function() {
commonTests({
method: 'function1'
});
});
describe('#function2', function() {
commonTests({
method: 'function2'
});
});
});
});
有没有办法做到这一点并保持 beforeEach 和 afterEach 的功能?
更新:
抱歉,看来我的示例有误。这是失败的情况:
define(['modules/MyModule'], function(MyModule) {
function commonTests(params) {
it('will fail because params.module is undefined', function() {
expect(typeof params.module).toBe('object');
expect(typeof params.module[params.method]).toBe('function');
});
it('has a few tests in here', function() {
expect(true).toBe(true);
});
}
describe('MyModule', function() {
var module;
beforeEach(function() {
module = new MyModule();
});
describe('#function1', function() {
commonTests({
module: module,
method: 'function1'
});
});
describe('#function2', function() {
commonTests({
module: module,
method: 'function2'
});
});
});
});
我认为它失败了,因为 module 的值被保留为调用 commonTests 的一部分,而不是始终使用 module 的当前值> 和第一个例子一样。当我到达那里时,我会发布我的解决方案...
最佳答案
感谢 Andreas 指出我的第一个示例确实有效!我使用的最终解决方案非常相似:
define(['modules/MyModule'], function(MyModule) {
var module;
function commonTests(params) {
it('will not fail because module is shared', function() {
expect(typeof module).toBe('object');
expect(typeof module[params.method]).toBe('function');
});
it('has a few tests in here', function() {
expect(true).toBe(true);
});
}
describe('MyModule', function() {
beforeEach(function() {
module = new MyModule();
});
describe('#function1', function() {
commonTests({
method: 'function1'
});
});
describe('#function2', function() {
commonTests({
method: 'function2'
});
});
});
});
尽管如果您需要能够将 module 作为参数传递给 commonTests,您将不得不采取稍微不同的方法,并且每个方法都有不同的函数它 block :
define(['modules/MyModule'], function(MyModule) {
var module;
function commonTest1(params) {
expect(typeof module).toBe('object');
expect(typeof module[params.method]).toBe('function');
}
function commonTest2(params) {
expect(true).toBe(true);
}
describe('MyModule', function() {
beforeEach(function() {
module = new MyModule();
});
describe('#function1', function() {
it('will not fail because module is shared', function() {
commonTest1({ method: 'function1' });
});
it('has a few tests in here', function() {
commonTest2({ method: 'function1' });
});
});
describe('#function2', function() {
it('will not fail because module is shared', function() {
commonTest1({ method: 'function2' });
});
it('has a few tests in here', function() {
commonTest2({ method: 'function2' });
});
});
});
});
这样,包含公共(public)测试的函数的执行会延迟到 beforeEach 运行其回调之后。
关于javascript - 是否可以在一个函数中定义一个 Jasmine 规范并且仍然有 beforeEach 应用于它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13139977/
类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc
给定这段代码defcreate@upgrades=User.update_all(["role=?","upgraded"],:id=>params[:upgrade])redirect_toadmin_upgrades_path,:notice=>"Successfullyupgradeduser."end我如何在该操作中实际验证它们是否已保存或未重定向到适当的页面和消息? 最佳答案 在Rails3中,update_all不返回任何有意义的信息,除了已更新的记录数(这可能取决于您的DBMS是否返回该信息)。http://ar.ru
使用带有Rails插件的vim,您可以创建一个迁移文件,然后一次性打开该文件吗?textmate也可以这样吗? 最佳答案 你可以使用rails.vim然后做类似的事情::Rgeneratemigratonadd_foo_to_bar插件将打开迁移生成的文件,这正是您想要的。我不能代表textmate。 关于ruby-使用VimRails,您可以创建一个新的迁移文件并一次性打开它吗?,我们在StackOverflow上找到一个类似的问题: https://sta
我需要从一个View访问多个模型。以前,我的links_controller仅用于提供以不同方式排序的链接资源。现在我想包括一个部分(我假设)显示按分数排序的顶级用户(@users=User.all.sort_by(&:score))我知道我可以将此代码插入每个链接操作并从View访问它,但这似乎不是“ruby方式”,我将需要在不久的将来访问更多模型。这可能会变得很脏,是否有针对这种情况的任何技术?注意事项:我认为我的应用程序正朝着单一格式和动态页面内容的方向发展,本质上是一个典型的网络应用程序。我知道before_filter但考虑到我希望应用程序进入的方向,这似乎很麻烦。最终从任何
我想要做的是有2个不同的Controller,client和test_client。客户端Controller已经构建,我想创建一个test_clientController,我可以使用它来玩弄客户端的UI并根据需要进行调整。我主要是想绕过我在客户端中内置的验证及其对加载数据的管理Controller的依赖。所以我希望test_clientController加载示例数据集,然后呈现客户端Controller的索引View,以便我可以调整客户端UI。就是这样。我在test_clients索引方法中试过这个:classTestClientdefindexrender:template=>
查看Ruby的CSV库的文档,我非常确定这是可能且简单的。我只需要使用Ruby删除CSV文件的前三列,但我没有成功运行它。 最佳答案 csv_table=CSV.read(file_path_in,:headers=>true)csv_table.delete("header_name")csv_table.to_csv#=>ThenewCSVinstringformat检查CSV::Table文档:http://ruby-doc.org/stdlib-1.9.2/libdoc/csv/rdoc/CSV/Table.html
这个问题在这里已经有了答案:Checktoseeifanarrayisalreadysorted?(8个答案)关闭9年前。我只是想知道是否有办法检查数组是否在增加?这是我的解决方案,但我正在寻找更漂亮的方法:n=-1@arr.flatten.each{|e|returnfalseife
我发现ActiveRecord::Base.transaction在复杂方法中非常有效。我想知道是否可以在如下事务中从AWSS3上传/删除文件:S3Object.transactiondo#writeintofiles#raiseanexceptionend引发异常后,每个操作都应在S3上回滚。S3Object这可能吗?? 最佳答案 虽然S3API具有批量删除功能,但它不支持事务,因为每个删除操作都可以独立于其他操作成功/失败。该API不提供任何批量上传功能(通过PUT或POST),因此每个上传操作都是通过一个独立的API调用完成的
如果您尝试在Ruby中的nil对象上调用方法,则会出现NoMethodError异常并显示消息:"undefinedmethod‘...’fornil:NilClass"然而,有一个tryRails中的方法,如果它被发送到一个nil对象,它只返回nil:require'rubygems'require'active_support/all'nil.try(:nonexisting_method)#noNoMethodErrorexceptionanymore那么try如何在内部工作以防止该异常? 最佳答案 像Ruby中的所有其他对象
关闭。这个问题需要detailsorclarity.它目前不接受答案。想改进这个问题吗?通过editingthispost添加细节并澄清问题.关闭8年前。Improvethisquestion为什么SecureRandom.uuid创建一个唯一的字符串?SecureRandom.uuid#=>"35cb4e30-54e1-49f9-b5ce-4134799eb2c0"SecureRandom.uuid方法创建的字符串从不重复?