Tag: rspec

我应该何时在Cucumber和RSpec工作流程中单独测试视图?

经过一段时间的Cucumber和RSpec BDD,我意识到我的许多黄瓜function只是更高级别的视图测试。 当我开始编写我的场景然后转到RSpec时,我不会编写视图规范,因为我可以复制并粘贴部分场景,这将是丑陋的复制。 以这种情况为例 Scenario: New user comes to the site Given I am not signed in When I go to the home page Then I should see “Sign up free” 我知道这不是直接测试视图,但是编写单独的视图规范来检查同样的事情对我来说似乎是多余的。 我接近黄瓜错了吗? 我究竟应该在视图规格中测试什么? 我应该为每个视图编写它们,例如测试视图的操作 def show @project = current_user.projects.first end 或者我应该只测试更复杂的视图?

应该是rspec匹配器:on =>:create

我正在使用一些Shoulda rspec匹配器来测试我的模型,其中一个是: describe Issue do it { should_not allow_value(“test”).for(:priority) } end 我的问题是我的模型中的validation如下所示: validates_format_of :priority, :with => /^(Low|Normal|High|Urgent)$/, :on => :update 因此,在运行此测试时,我得到: 1) ‘Issue should not allow priority to be set to “test”‘ FAILED Expected errors when priority is set to “test”, got errors: category is invalid (nil)title can’t be blank (nil)profile_id can’t be blank (nil) […]

运行我的控制器的rspec测试时出现Rspec错误

我正在开发一个Rails v2.3应用程序。 当我通过执行命令运行rspec测试时: rspec spec/controllers/my_controller_spec.rb 我收到了以下错误消息 : /.rvm/gems/ruby-1.8.7-p352@myapp/gems/rspec-core-2.6.4/lib/rspec/core/configuration.rb:427:in `raise_if_rspec_1_is_loaded’: (RuntimeError) ******************************************************************************** You are running rspec-2, but it seems as though rspec-1 has been loaded as well. This is likely due to a statement like this somewhere in the specs: require ‘spec’ Please locate that statement, remove it, and try again. ******************************************************************************** /.rvm/gems/ruby-1.8.7-p352@myapp/gems/rspec-core-2.6.4/lib/rspec/core/configuration.rb:420:in `load_spec_files’ —-更新— 因为它抱怨某些包含代码的文件require […]

在辅助规范中存储控制器辅助方法

在我的application_controller.rb : helper_method :current_brand def current_brand @brand ||= Brand.find_by_organization_id(current_user.organization_id) end 在我的帮助者something_helper.rb def brands return [] unless can? :read, Brand # current_brand is called end 我正在为something_helper编写一个规范,并希望将stub_brand存根 describe SomethingHelper do before :each do helper.stub!(:can?).and_return(true) # This stub works end it “does the extraordinary” do brand = Factory.create(:brand) helper.stub!(:current_brand).and_return(brand) # This stub doesnt work helper.brands.should_not be_empty end end […]

Rspec型号规格

如何使用Rspec覆盖以下方法? def validate if !self.response.blank? && !self.response.match(“<").nil? self.errors.add :base, 'Please ensure that Response field do not contain HTML() tags’ end end 有人可以帮忙吗?

为什么我不能从Rspec测试中访问Rails的URL助手?

最初我用Cucumber和Capybara写了我的测试,这很好。 然后我转而使用Rpsec和Capybara,这也很好。 然后我在尝试让资产管道工作时删除了我的gemlock文件,突然我的测试中的URL帮助程序停止工作。 示例错误: Failure/Error: visit report_areas_path(@report) NoMethodError: undefined method `visit’ for # # ./spec/features/area_spec.rb:12:in `block (3 levels) in ‘ 我想让URL助手再次工作,这样我的测试可以指导我完成资产管道的实施。 我怎样才能解决这个问题? 我的所有测试都在/ spec / features中。 相关软件如下…… /spec/spec_helper.rb: require ‘rubygems’ require ‘spork’ Spork.prefork do end Spork.each_run do # This code will be run each time you run your specs. end ENV[“RAILS_ENV”] ||= ‘test’ require File.expand_path(“../../config/environment”, […]

spec with guard,rails 3.1.1和ruby 1.9.3获取无法加载此类文件错误

我刚刚在macbook air上安装了rvm和所有好东西,我用ruby 1.9.3启动了我的rails 3.1.1应用程序。 在我的gem文件中,我放下了警卫和rspec,现在当我尝试单独运行rspec或使用警卫时,我会向我发出错误并且我不确定为什么, 我运行时得到的输出是这样的, phantom:tasks maxmarze$ guard Please install rb-fsevent gem for Mac OSX FSEvents support Using polling (Please help us to support your system better than that). Please install growl_notify or growl gem for Mac OS X notification support and add it to your Gemfile Guard is now watching at ‘/Users/maxmarze/rails_projects/tasks’ Guard::RSpec […]

Rails 4中的“Factory not registered”错误

我在Rails 4中使用Factory Girl获得了“Factory not registered”错误。这是我在spec / factories / user.rb中为一个简单的Devise用户定义的工厂: FactoryGirl.define do factory :user do email ‘test@example.com’ password ‘foobar’ password_confirmation ‘foobar’ end end 这是spec_helper.rb: ENV[“RAILS_ENV”] ||= ‘test’ require File.expand_path(“../../config/environment”, __FILE__) require ‘rspec/rails’ require ’email_spec’ require ‘rspec/autorun’ require ‘factory_girl’ Dir[Rails.root.join(“spec/support/**/*.rb”)].each { |f| require f } ActiveRecord::Migration.check_pending! if defined?(ActiveRecord::Migration) RSpec.configure do |config| config.include(EmailSpec::Helpers) config.include(EmailSpec::Matchers) config.fixture_path = “#{::Rails.root}/spec/fixtures” config.use_transactional_fixtures […]

Rails教程:RSpec测试解耦

我正在尝试在Michael Hartl的Ruby on Rails教程中 进行第8.5章练习2 。 演习如下: 按照第8.3.3节中的示例,浏览用户和身份validation请求规范(即当前在spec / requests目录中的文件)并在spec / support / utilities.rb中定义实用程序函数,以将测试与实现分离。 额外功劳:将支持代码组织到单独的文件和模块中,并通过在规范帮助文件中正确包含模块来使一切工作正常。 例8.3.3:utilities.rb include ApplicationHelper def valid_signin(user) fill_in “Email”, with: user.email fill_in “Password”, with: user.password click_button “Sign in” end RSpec::Matchers.define :have_error_message do |message| match do |page| page.should have_selector(‘div.alert.alert-error’, text: message) end end 定义的valid_signin(user)函数在authentication_pages_spec.rb的以下块中使用,并且工作正常。 describe “with valid information” do let(:user){FactoryGirl.create(:user)} before { valid_signin(user) […]

臭名昭着的AbstractController :: ActionNotFound – Devise + Rails

所以我已经阅读了如何解决这个问题: 自定义设计会话控制器的RSpec测试因AbstractController :: ActionNotFound失败 和 http://lostincode.net/blog/testing-devise-controllers 但我在哪个文件下添加这些更改是我的问题: 在我的rspec文件夹下 registrations_controller 我试过这个 before :each do request.env[‘devise.mapping’] = Devise.mappings[:user] end require ‘spec_helper’ describe RegistrationsController do describe “GET ‘edit'” do it “should be successful” do get ‘edit’ response.should be_success end end end 哪个不起作用,任何帮助改变特定文件以使这项工作将不胜感激。 编辑 所以我也尝试过 – https://github.com/plataformatec/devise/wiki/How-To:-Controllers-and-Views-tests-with-Rails-3-(and-rspec) 所以我创建了一个带有spec / support的文件夹,并创建了一个名为controllers_macros.rb的文件 module ControllerMacros def login_admin before(:each) do @request.env[“devise.mapping”] = Devise.mappings[:admin] […]