Tag: rspec

如何在functionrspec中添加上下文?

我想在我的functionrspec中添加上下文,但我不确定上下文和场景之间有什么意义。 这是我到目前为止: feature ‘As an admin I manage the orders of the system’ do context ‘Logged in user who is an admin’ do before(:each) do admin = create(:admin_user) login_as(admin, :scope => :user) end scenario ‘User sees all the orders’ do visit admin_orders_path expect(page).to have_content(‘List of orders’) end end context ‘Logged in user who is not […]

之前(:上下文)确定范围是否适用于儿童情境?

在下面的代码示例中, before块在父和子contexts 之前运行,因此是3次? 或者只是父母,因此1次? 即范围有多深? describe “my spec class” do before(:context) do do stuff end context “parent context” do context “subcontext” do stuff… end context “subcontext” do stuff….. end end 谢谢您的帮助。

如何告诉自动测试正确跟踪应用程序源中的更改?

我希望每当我的某个rails应用程序的相关文件发生变化时,我都会自动测试以进行牛排验收测试。 在研究了Rspec和Cucumber自己的自动测试配置后,我正在尝试以下映射: Autotest.add_hook :initialize do |at| at.add_mapping(%r%^spec/acceptance/.*_spec.rb$%, true) { |filename, _| filename } at.add_mapping(%r%^app/(models|controllers|helpers|lib)/. rb$%) { at.files_matching %r%^spec/acceptance/. _spec.rb$% } at.add_mapping(%r%^app/views/(.*)/. rb$%) { at.files_matching %r%^spec/acceptance/. _spec.rb$% } end Autotest.add_hook :initialize do |at| at.add_mapping(%r%^spec/acceptance/.*_spec.rb$%, true) { |filename, _| filename } at.add_mapping(%r%^app/(models|controllers|helpers|lib)/. rb$%) { at.files_matching %r%^spec/acceptance/. _spec.rb$% } at.add_mapping(%r%^app/views/(.*)/. rb$%) { at.files_matching %r%^spec/acceptance/. _spec.rb$% } end Autotest.add_hook […]

测试在我的Ubuntu pc上运行,但不在我的Mac OS X Lion 10.7.3,Ruby 1.9.3,Rails 3.2.1上运行

我在Dropbox中有项目和两台正在运行的笔记本电脑:一台使用Ubuntu,另一台使用Mac OS X Lion 10.7.3。 当我尝试在ubuntu上运行rspec或者黄瓜时,它们运行良好,但是当我尝试在Mac上运行测试时,我在config / application.rb中 Bundler.require(*Rails.groups(:assets => %w(development test)))了相同的错误:13: Bundler.require(*Rails.groups(:assets => %w(development test))) 黄瓜: Using the default profile… undefined method `gsub’ for nil:NilClass (NoMethodError) /Users/sergey/.rvm/gems/ruby-1.9.3-p125@global/gems/bundler-1.1.1/lib/bundler/runtime.rb:77:in `rescue in rescue in block in require’ /Users/sergey/.rvm/gems/ruby-1.9.3-p125@global/gems/bundler-1.1.1/lib/bundler/runtime.rb:72:in `rescue in block in require’ /Users/sergey/.rvm/gems/ruby-1.9.3-p125@global/gems/bundler-1.1.1/lib/bundler/runtime.rb:62:in `block in require’ /Users/sergey/.rvm/gems/ruby-1.9.3-p125@global/gems/bundler-1.1.1/lib/bundler/runtime.rb:55:in `each’ /Users/sergey/.rvm/gems/ruby-1.9.3-p125@global/gems/bundler-1.1.1/lib/bundler/runtime.rb:55:in `require’ /Users/sergey/.rvm/gems/ruby-1.9.3-p125@global/gems/bundler-1.1.1/lib/bundler.rb:119:in `require’ /Users/sergey/Dropbox/rails_projects/payforapps/config/application.rb:13:in `’ /Users/sergey/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require’ […]

RSpec控制器测试错误:控制器没有实现方法

我为Devise注册编写了一些自定义代码。 即,一旦用户点击“注册”按钮,Devise的通用代码可确保正确保存用户。 但是,我想进入并确保用户获得相关的Stripe客户帐户。 下面的大部分代码来自Devise,除了我添加的几行代码: class Users::RegistrationsController < Devise::RegistrationsController def create build_resource(sign_up_params) resource.save yield resource if block_given? if resource.persisted? #### If statement below is the key custom code #### if create_stripe_customer #### Back to default Devise code #### if resource.active_for_authentication? set_flash_message :success, :signed_up if is_flashing_format? sign_up(resource_name, resource) respond_with resource, location: after_sign_up_path_for(resource) else set_flash_message :notice, :"signed_up_but_#{resource.inactive_message}" if […]

如何保持控制器规格及其模拟DRY?

我被告知这是编写它阻止的控制器的正确方法: describe UsersController do let(:user){ mock_model(User, id: 2, name: “Jimbo”, email: ‘jimbo@email.com’, password: ‘passwordhuzzah’, password_confirmation: ‘passwordhuzzah’) } describe ‘PATCH #update’ do it “should fail in this case” do User.should_receive(:find).with(user.id.to_s).and_return user user.should_receive(:update_attributes).with({ “email” => user.email, “name” => user.name, “password” => user.password, “password_confirmation” => user.password_confirmation }).and_return true patch :update, id: user.id, user: { email: user.email, name: user.name, […]

模型在规格之前运行

我有以下spec文件: describe ‘before_save autofill country code’ do it “should autofill country code” do empty_country_code = nil @store = Factory.build(:store, :country_code => empty_country_code) expect{ @store.save }.to change { @store.country_code }.from(nil).to(‘1’) end end 工厂定义如下: Factory.define :store do |store| store.city “New York” store.country_code “81” end 我的模型有以下几行: before_save :autofill_country_code 哪里 def autofill_country_code unless self.country_code.present? country = GeoCache.geocode(self.city).country_code country_code = […]

如何编写复杂轨道索引的集成测试(最佳实践)

假设我有一个列出文章的页面。 控制器中的代码曾经是 # articles#index @articles = Article.paginate(page: params[:page], per_page: 10, order: :title) 我的测试就像 # spec/requests/article_pages_spec Article.paginate(page: 1, per_page:10, order: :title).each do |a| a.should have_selector(‘h3’, text: a.title) end 好的。 现在我的代码改变了一堆。 索引就像 @articles = Article.find(:all, conditions: complicated_status_conditions) .sort_by { |a| complicated_weighted_rating_stuff } .select { |a| complicated_filters } .paginate(…) 或者其他的东西。 那么我的请求规范现在应该是什么样的呢? 我不想只是将应用程序代码复制并粘贴到测试中,但同时,条件和顺序现在相当复杂,因此测试所有预期元素的存在和顺序肯定会失败,除非我模仿索引控制器。 最好的方法是什么,避免如此具体地进行测试,复制应用程序代码? 将查询重构为某个中心位置(如模型)并在测试中重复使用它?

黄瓜和自定义RSpec匹配器

我正在尝试为黄瓜编写一个自定义RSpec匹配器。 我在env.rb中需要cucumber / rails / rspec,但我仍然得到“未初始化的常量Spec :: Matchers”错误。 我正在使用最新版本的Rspec,Cucumber和CucumberRails。 我错过了什么? PS相同的匹配器与纯RSpec一起工作正常…

使用factory_girl进行RSpec测试,并使用will_paginate进行关联对象

我在rspec中为will_paginate编写测试时遇到问题。 问题是我有一个拥有许多宠物的主人的模型。 这导致一个看起来像这样的factories.rb文件: Factory.define :owner do |owner| owner.personid “1111111” owner.firstname “Nisse” owner.lastname “Gunnarsson” owner.street “Street” owner.postaladress “38830” owner.town “Town” owner.phone “555-5555” owner.mobile “555-5556” owner.email “nisse@test.com” owner.reminder true end Factory.define :pet do |pet| pet.name “Hedvig” pet.specie “Rabbot” pet.breed “Lowen/vadur” pet.colour “Madagaskar” pet.association :owner end 在我的测试中,我有 describe “Get show” do before(:each) do @owner = Factory(:owner) 30.times do […]