在 rspec 中你可以这样做:
let(:input) { 'foo' }
before_each do
setup_some_thing(input)
end
context 'when input is bar do
let(:input) { 'bar' }
it 'does something different' do
end
end
context 'when input is baz do
let(:input) { 'baz' }
it 'does something else different' do
end
end
这允许您将大型对象的方法调用或实例化定义为其较小部分的总和。然后,您可以在不同的上下文中覆盖那些单独的小部分。这个想法是您在每次测试之前创建一条快乐路径,然后在您的上下文 block 中指定与快乐路径的偏差。
不幸的是,我似乎无法用 Jest 做到这一点。我尝试了以下方法:
beforeEach(() => {
let input = 'foo';
beforeEach(() => {
setupSomeThing(input);
});
describe('when input is bar', () => {
input = 'bar';
it('does something different', () => {
});
});
describe('when input is baz', () => {
input = 'baz';
it('does something different', () => {
});
});
});
因为 jest 在运行任何特定的描述 block 之前会执行每个描述 block ,所以输入始终为“baz”。有谁知道解决方法或获得 rspec 行为的方法?
提前致谢!
您可以使用 beforeAll 获得类似的行为(尽管没有惰性评估)。
beforeEach(() => {
let input = 'foo';
beforeEach(() => {
setupSomeThing(input);
});
describe('when input is bar', () => {
beforeAll(() => {
input = 'bar';
});
it('does something different', () => {
});
});
describe('when input is baz', () => {
beforeAll(() => {
input = 'baz';
});
it('does something different', () => {
});
});
});
最佳答案
我找到的最好的解决方案是像这样的库
https://github.com/stalniy/bdd-lazy-var
和
https://github.com/tatyshev/given2
如果您不想引入依赖项,您可以通过执行以下操作获得类似的行为(尽管没有惰性评估):
beforeEach(() => {
let input = 'foo';
beforeEach(() => {
setupSomeThing(input);
});
describe('when input is bar', () => {
beforeAll(() => {
input = 'bar';
});
it('does something different', () => {
});
});
describe('when input is baz', () => {
beforeAll(() => {
input = 'baz';
});
it('does something different', () => {
});
});
});
关于javascript - Jest 等同于 RSpec 惰性评估变量 (let)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44141174/
我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou
我正在编写一个包含C扩展的gem。通常当我写一个gem时,我会遵循TDD的过程,我会写一个失败的规范,然后处理代码直到它通过,等等......在“ext/mygem/mygem.c”中我的C扩展和在gemspec的“扩展”中配置的有效extconf.rb,如何运行我的规范并仍然加载我的C扩展?当我更改C代码时,我需要采取哪些步骤来重新编译代码?这可能是个愚蠢的问题,但是从我的gem的开发源代码树中输入“bundleinstall”不会构建任何native扩展。当我手动运行rubyext/mygem/extconf.rb时,我确实得到了一个Makefile(在整个项目的根目录中),然后当
我正在查看instance_variable_set的文档并看到给出的示例代码是这样做的:obj.instance_variable_set(:@instnc_var,"valuefortheinstancevariable")然后允许您在类的任何实例方法中以@instnc_var的形式访问该变量。我想知道为什么在@instnc_var之前需要一个冒号:。冒号有什么作用? 最佳答案 我的第一直觉是告诉你不要使用instance_variable_set除非你真的知道你用它做什么。它本质上是一种元编程工具或绕过实例变量可见性的黑客攻击
我有一些Ruby代码,如下所示:Something.createdo|x|x.foo=barend我想编写一个测试,它使用double代替block参数x,这样我就可以调用:x_double.should_receive(:foo).with("whatever").这可能吗? 最佳答案 specify'something'dox=doublex.should_receive(:foo=).with("whatever")Something.should_receive(:create).and_yield(x)#callthere
如何使用RSpec::Core::RakeTask初始化RSpecRake任务?require'rspec/core/rake_task'RSpec::Core::RakeTask.newdo|t|#whatdoIputinhere?endInitialize函数记录在http://rubydoc.info/github/rspec/rspec-core/RSpec/Core/RakeTask#initialize-instance_method没有很好的记录;它只是说:-(RakeTask)initialize(*args,&task_block)AnewinstanceofRake
我花了三天的时间用头撞墙,试图弄清楚为什么简单的“rake”不能通过我的规范文件。如果您遇到这种情况:任何文件夹路径中都不要有空格!。严重地。事实上,从现在开始,您命名的任何内容都没有空格。这是我的控制台输出:(在/Users/*****/Desktop/LearningRuby/learn_ruby)$rake/Users/*******/Desktop/LearningRuby/learn_ruby/00_hello/hello_spec.rb:116:in`require':cannotloadsuchfile--hello(LoadError) 最佳
我注意到像bundler这样的项目在每个specfile中执行requirespec_helper我还注意到rspec使用选项--require,它允许您在引导rspec时要求一个文件。您还可以将其添加到.rspec文件中,因此只要您运行不带参数的rspec就会添加它。使用上述方法有什么缺点可以解释为什么像bundler这样的项目选择在每个规范文件中都需要spec_helper吗? 最佳答案 我不在Bundler上工作,所以我不能直接谈论他们的做法。并非所有项目都checkin.rspec文件。原因是这个文件,通常按照当前的惯例,只
我正在编写一个gem,我必须在其中fork两个启动两个webrick服务器的进程。我想通过基类的类方法启动这个服务器,因为应该只有这两个服务器在运行,而不是多个。在运行时,我想调用这两个服务器上的一些方法来更改变量。我的问题是,我无法通过基类的类方法访问fork的实例变量。此外,我不能在我的基类中使用线程,因为在幕后我正在使用另一个不是线程安全的库。所以我必须将每个服务器派生到它自己的进程。我用类变量试过了,比如@@server。但是当我试图通过基类访问这个变量时,它是nil。我读到在Ruby中不可能在分支之间共享类变量,对吗?那么,还有其他解决办法吗?我考虑过使用单例,但我不确定这是
Sinatra新手;我正在运行一些rspec测试,但在日志中收到了一堆不需要的噪音。如何消除日志中过多的噪音?我仔细检查了环境是否设置为:test,这意味着记录器级别应设置为WARN而不是DEBUG。spec_helper:require"./app"require"sinatra"require"rspec"require"rack/test"require"database_cleaner"require"factory_girl"set:environment,:testFactoryGirl.definition_file_paths=%w{./factories./test/
关闭。这个问题需要detailsorclarity.它目前不接受答案。想改进这个问题吗?通过editingthispost添加细节并澄清问题.关闭8年前。Improvethisquestion在首页我有:汽车:VolvoSaabMercedesAudistatic_pages_spec.rb中的测试代码:it"shouldhavetherightselect"dovisithome_pathit{shouldhave_select('cars',:options=>['volvo','saab','mercedes','audi'])}end响应是rspec./spec/request