Tag: rspec

Date与ActiveSupport :: TimeWithZone的比较失败

我的Waiver模型上有一个age方法,如下所示: def age(date = nil) if date.nil? date = Date.today end age = 0 unless date_of_birth.nil? age = date.year – date_of_birth.year age -= 1 if date < date_of_birth + age.years #for days before birthday end return age end 然后我有一个看起来像这样的规范: it “calculates the proper age” do waiver = FactoryGirl.create(:waiver, date_of_birth: 12.years.ago) waiver.age.should == 12 end 当我运行此规范时,我得到comparison […]

我如何获得RSpec的共享示例,例如Ruby Test :: Unit中的行为?

是否有类似于RSpec for test :: Unit测试中的shared_examples的插件/扩展名?

使用Rspec进行测试时Zeus失败了

当我运行此命令时: rspec spec/controllers/messages_controller_spec.rb 我明白了: 完成0.54446秒 2个例子,0个失败 随机种子6226 很好。 但是,当我运行此命令时: zeus rspec spec/controllers/messages_controller_spec.rb 我明白了: 完成0.5356秒 2个例子,0个失败 随机种子0 这不是问题,但我马上也得到了这个: F。 失败: 1)MessagesController POST #create创建一个带有HTTP 200状态代码的消息 Failure/Error: post :create, message: “test message” NameError: undefined method `post’ for # # ./spec/controllers/messages_controller_spec.rb:10:in `block (3 levels) in ‘ # -e:1:in `’ 在0.10724秒完成2个示例,1个失败失败示例: rspec ./spec/controllers/messages_controller_spec.rb:9#MessagesController POST #create使用HTTP 200状态代码创建一条消息

黄瓜:在所有情况下钩子只运行一次之前

我有一个包含多个场景的场景大纲。 我希望我的Before钩子只运行一次所以我可以引导我需要针对所有场景运行的ActiveRecord对象。 问题是如果我使用 Before do # my code here end 这将在每个场景之前执行。 反正有没有为整个大纲运行一次?

FactoryGirl覆盖关联对象的属性

这可能很简单,但我无法在任何地方找到一个例子。 我有两个工厂: FactoryGirl.define do factory :profile do user title “director” bio “I am very good at things” linked_in “http://my.linkedin.profile.com” website “www.mysite.com” city “London” end end FactoryGirl.define do factory :user do |u| u.first_name {Faker::Name.first_name} u.last_name {Faker::Name.last_name} company ‘National Stock Exchange’ u.email {Faker::Internet.email} end end 我想要做的是在创建配置文件时覆盖一些用户属性: p = FactoryGirl.create(:profile, user: {email: “test@test.com”}) 或类似的东西,但我不能正确的语法。 错误: ActiveRecord::AssociationTypeMismatch: User(#70239688060520) expected, […]

如何模拟Net :: HTTP :: Post?

是的,我知道最好使用webmock,但我想知道如何在RSpec中模拟这个方法: def method_to_test url = URI.parse uri req = Net::HTTP::Post.new url.path res = Net::HTTP.start(url.host, url.port) do |http| http.request req, foo: 1 end res end 这是RSpec: let( :uri ) { ‘http://example.com’ } specify ‘HTTP call’ do http = mock :http Net::HTTP.stub!(:start).and_yield http http.should_receive(:request).with(Net::HTTP::Post.new(uri), foo: 1) .and_return 202 method_to_test.should == 202 end 测试失败,因为似乎试图匹配NET :: HTTP :: Post对象: […]

荒谬地运行rspec测试

我有这个非常简单和短暂的测试,需要80秒才能运行,有关加速的建议吗? require ‘spec_helper’ describe “Battles” do let(:character) { (FactoryGirl.create :character) } describe “Battle button” do it “redirects to battle screen” do character.init(“fighter”) click(“Battle”) expect(page).to have_content character.name end end end 的Gemfile source ‘https://rubygems.org’ ruby ‘2.0.0’ gem ‘pg’, ‘0.15.1’ gem ‘rails’, ‘4.0.2’ gem ‘bcrypt-ruby’, ‘~> 3.1.2’ gem ‘twitter-bootstrap-rails’ gem ‘therubyracer’ gem ‘devise’ gem ‘less-rails’ gem ‘will_paginate’, ‘~> […]

在rspec中没有正确加载to_yaml文件(rails项目)

我正在尝试将我的开发sqlite3数据库导出到我的测试数据库中。 为了做到这一点,我首先将我的模型导出到rails控制台并将其保存到文件中。 > MyModel.all.to_yaml # this was saved to mymodels.yml 现在当我运行rspec时,它在尝试解析mymodels.yml时失败了。 我得到的错误是: Failure/Error: Unable to find matching line from backtrace a YAML error occurred parsing /Users/MakeM/MyProject1/spec/fixtures/mymodels.yml. 请注意,YAML必须始终使用空格缩进。 不允许使用标签。 请查看http://www.yaml.org/faq.html确切的错误是:NoMethodError:nil的未定义方法`keys’:NilClass 知道我为什么会收到这个错误吗? 我应该提到的一点是,to_yaml输出的yaml对我来说有点奇怪。 这是它的一部分: — – !ruby/object:MyModel attributes: id: 133 book: FirstBook chapters: 50 created_at: 2010-10-06 05:03:15.709931 updated_at: 2010-10-06 05:03:15.709931 abbr: FB attributes_cache: {} changed_attributes: {} destroyed: […]

初始watir-webdriver启动后边缘浏览器崩溃

我正在使用最新的selenium-webdriver(2.47.1)和watir-webdriver(0.8.0),并将Edge WebDriver安装在Ruby / bin文件夹中,就像其他webdriver.exe文件一样; 像chromedriver或iedriver。 它似乎启动了Edge浏览器,但在关闭浏览器并且未通过所有测试之前不会尝试转到URL。 我在Windows中的防火墙允许它,当我运行我的rspec测试时它指出: Selenium::WebDriver::Error::NoSuchWindowError: no such window 有没有人让MSWebdriver与watir-webdriver一起工作? 错误: c:/Ruby-222-x64/lib/ruby/gems/2.2.0/gems/selenium-webdriver-2.47.1/lib/selenium/webdriver/remote/response.rb:71:in `assert_ok’: not implemented (Selenium::WebDriver::Error::WebDriverError) from c:/Ruby-222-x64/lib/ruby/gems/2.2.0/gems/selenium-webdriver-2.47.1/lib/selenium/webdriver/remote/response.rb:34:in `initialize’ from c:/Ruby-222-x64/lib/ruby/gems/2.2.0/gems/selenium-webdriver-2.47.1/lib/selenium/webdriver/remote/http/common.rb:78:in `new’ from c:/Ruby-222-x64/lib/ruby/gems/2.2.0/gems/selenium-webdriver-2.47.1/lib/selenium/webdriver/remote/http/common.rb:78:in `create_response’ from c:/Ruby-222-x64/lib/ruby/gems/2.2.0/gems/selenium-webdriver-2.47.1/lib/selenium/webdriver/remote/http/default.rb:90:in `request’ from c:/Ruby-222-x64/lib/ruby/gems/2.2.0/gems/selenium-webdriver-2.47.1/lib/selenium/webdriver/remote/http/common.rb:59:in `call’ from c:/Ruby-222-x64/lib/ruby/gems/2.2.0/gems/selenium-webdriver-2.47.1/lib/selenium/webdriver/remote/bridge.rb:657:in `raw_execute’ from c:/Ruby-222-x64/lib/ruby/gems/2.2.0/gems/selenium-webdriver-2.47.1/lib/selenium/webdriver/remote/bridge.rb:635:in `execute’ from c:/Ruby-222-x64/lib/ruby/gems/2.2.0/gems/selenium-webdriver-2.47.1/lib/selenium/webdriver/remote/bridge.rb:221:in `switchToDefaultContent’ from c:/Ruby-222-x64/lib/ruby/gems/2.2.0/gems/selenium-webdriver-2.47.1/lib/selenium/webdriver/common/target_locator.rb:96:in `default_content’ from c:/Ruby-222-x64/lib/ruby/gems/2.2.0/gems/watir-webdriver-0.8.0/lib/watir-webdriver/browser.rb:385:in `assert_exists’ from c:/Ruby-222-x64/lib/ruby/gems/2.2.0/gems/watir-webdriver-0.8.0/lib/watir-webdriver/browser.rb:110:in `url’ from c:/Ruby-222-x64/lib/ruby/gems/2.2.0/gems/watir-webdriver-0.8.0/lib/watir-webdriver/browser.rb:79:in `goto’ from […]

使用guard-rspec与factory-girl-rails

我正在使用factory_girl_rails替代我的Rails项目中的灯具。 我也使用guard-rspec自动运行我的规格。 Guard没有接受我对工厂文件所做的更改(例如spec/factories/users.rb ) – 我必须停止保护,然后重新初始化它以获取这些更改。 有哪些选项可以避免这种手动过程?