Tag: rspec

Rails:良好的Rspec2示例用法? (另外:Cucumber,Pickle,Capybara)

我正在寻找一个使用Rspec 2作为测试库的最新开源应用程序。 我想看看有经验的开发人员如何正确地利用库来测试完整的堆栈,因为我对自己的知识一直存在疑问(来自testunit,部分原因是由于最新Rspec版本的相当稀疏的文档,甚至虽然它不断改进)。 如果一个项目使用Cucumber,Pickle和/或Capybara以及Rspec 2,你会让我高兴得跳起来。 有什么指针吗? 干杯!

RSpec允许/期望vs期望/和_return

在RSpec中,特别是版本> = 3,之间有什么区别: 使用allow来设置返回测试双精度的参数的消息期望,然后使用expect对返回的测试双精度进行断言 只需使用expect来设置参数的期望并返回测试double 或者只是语义学? 我知道提供/指定带有expect的返回值是RSpec模拟2.13中的语法 ,但据我所知,RSpec模拟3中的语法改变为使用allow 。 但是,在下面的(传递)示例代码中,使用allow / expect或者只是expect / and_return似乎会生成相同的结果。 如果一种语法比另一种更受欢迎,也许我会期望有某种弃用通知,但由于没有,似乎两种语法都被认为是有效的: class Foo def self.bar(baz) # not important what happens to baz parameter # only important that it is passed in new end def qux # perform some action end end class SomethingThatCallsFoo def some_long_process(baz) # do some processing Foo.bar(baz).qux # […]

黄瓜没有找到步骤定义

我的黄瓜只是找不到步骤定义。 文件结构(只有Rails根目录下的specs文件夹)如下所示: -> specs -> features -> main_structure.feature -> step_definitions -> main_structure_steps.rb 这是main_structure.feature: Feature: Main structure Scenario: Viewing the Structure page When I am on the structure page 这是main_structure_steps.rb: When(/^I am on the structure page$/) do visit ‘/’ end 现在我像这样运行黄瓜命令: → cucumber spec/features -r features 我得到这个输出: Using the default profile… Feature: Main structure Scenario: Viewing […]

在Rack :: Test中使用Cookies

我正在尝试使用Rack :: Test为我的Sinatra应用程序编写RSpec测试。 我无法理解如何使用cookies。 例如,如果我的应用程序设置了cookie(而不是通过:session),我该如何检查该cookie是否正确设置? 另外,如何使用该cookie发送请求?

rails生成rspec:install config / environments / development.rb:1:在`’:undefined method`configure’

首先,这是我的版本: Greg-Nowickis-MacBook-Pro:sample_app Greg_Nowicki$ ruby -v ruby 2.0.0p451 (2014-02-24 revision 45167) [x86_64-darwin13.1.0] Greg-Nowickis-MacBook-Pro:sample_app Greg_Nowicki$ rails -v Rails 4.0.4 我正在关注Hartl Rails教程并安装rspec进行测试。 我已经将gem’rspec-rails’添加到我的gemfile中,当我运行rails生成rspec:install时,这就是我得到的: Greg-Nowickis-MacBook-Pro:sample_app Greg_Nowicki$ rails generate rspec:install /Users/Greg_Nowicki/workspace/railstutorial/sample_app/config/environments/development.rb:1:in `’: undefined method `configure’ for # (NoMethodError) from /Users/Greg_Nowicki/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/gems/activesupport-4.0.4/lib/active_support/dependencies.rb:229:in `require’ from /Users/Greg_Nowicki/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/gems/activesupport-4.0.4/lib/active_support/dependencies.rb:229:in `block in require’ from /Users/Greg_Nowicki/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/gems/activesupport-4.0.4/lib/active_support/dependencies.rb:214:in `load_dependency’ from /Users/Greg_Nowicki/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/gems/activesupport-4.0.4/lib/active_support/dependencies.rb:229:in `require’ from /Users/Greg_Nowicki/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/gems/railties-4.0.4/lib/rails/engine.rb:591:in `block (2 levels) in ‘ from […]

rspec失败错误:假设错误响应`false?`

我正在运行这部分测试: describe Dictionary do before do @d = Dictionary.new end it ‘can check whether a given keyword exists’ do @d.include?(‘fish’).should be_false end 使用此代码: class Dictionary def initialize @hash = {} end def add(new_entry) new_entry.class == String ? @hash[new_entry] = nil : new_entry.each { |noun, definition| @hash[noun] = definition} end def entries @hash end def keywords […]

如何在rspec特性测试中访问(设计)current_user?

在设计文档中,他们提供了有关在测试控制器时如何访问current_user的提示: https://github.com/plataformatec/devise/wiki/How-To:-Test-controllers-with-Rails-3-and-4-%28and-RSpec%29 但是,在进行function测试时呢? 我正在尝试测试我的一个控制器的create方法,并在该控制器中使用current_user变量。 问题是设计中建议的宏使用@request变量,对于特征规范它是零。 什么是变通方法? 编辑: 到目前为止,这是我目前的规格: feature ‘As a user I manage the orders of the system’ do scenario ‘User is logged in ad an admin’ do user = create(:user) order = create(:order, user: user) visit orders_path #Expectations end end 问题是在我的OrdersController我有一个current_user.orders调用,并且由于current_user没有定义,它会将我重定向到/users/sign_in 。 我在/spec/features/manage_orders.rb下定义了这个

RailsTutorial – 第8.4.3章 – 在集成测试中添加用户后,测试数据库未清除

我对这个很难过。 到目前为止,教程中的所有内容都进展顺利,但是当我将这段代码添加到我的/spec/requests/users_spec.rb文件中时,事情开始向南: describe “success” do it “should make a new user” do lambda do visit signup_path fill_in “Name”, :with => “Example User” fill_in “Email”, :with => “ryan@example.com” fill_in “Password”, :with => “foobar” fill_in “Confirmation”, :with => “foobar” click_button response.should have_selector(“div.flash.success”, :content => “Welcome”) response.should render_template(‘users/show’) end.should change(User, :count).by(1) end end 如果我清除测试数据库(rake db:test:prepare),则所有测试都通过。 但是,如果我再次运行测试,它们会失败,因为测试数据库不会清除上面添加的代码的记录。 我已经google了很多,我发现的大部分内容都指向config.use_transactional_fixtures设置,或指向代码中的嵌套问题。 我很确定这些都不是我的情况。 […]

如何在Capybara和RSpec中测试CSV文件下载?

以下是在控制器中: respond_to do |format| format.csv { send_data as_csv, type:’text/csv’ } end 在规格: click_link ‘Download CSV’ page.driver.browser.switch_to.alert.accept expect( page ).to have_content csv_data 但这不起作用: Failure/Error: page.driver.browser.switch_to.alert.accept Selenium::WebDriver::Error::NoAlertPresentError: No alert is present 我看到“保存文件”对话框显示,但显然它不是“警告”对话框。 如何单击确定并让Capybara查看数据?

在Ruby中测试STDIN

我目前正在尝试测试一个基本方法,它接收来自用户的一些输入(获取)并输出它(puts)。 经过一番研究后,我找到了测试标准输出流的好方法,如下所示: def capture_standard_output(&block) original_stream = $stdout $stdout = mock = StringIO.new yield mock.string.chomp ensure $stdout = original_stream end 我正在测试的方法是下面的一个,输出和输入是指我在开头初始化并指向等效的$ stdout&$ stdin的ivars: def ask_for_mark ouput.puts ‘What shall I call you today?’ answer = input.gets.chomp.capitalize answer end 现在我已经看到了STDIN的一些解决方案,但还没有真正了解它们中的任何一个,我绝对不想复制和粘贴。 我唯一能够“工作”的是下面的那个,但它并没有真正起作用,因为当我运行rspec它暂停并等待输入时,只需按Enter键,它就会通过: it “takes user’s name and returns it” do output = capture_standard_output { game.ask_for_name } expect(output).to eq “What […]