Tag: rspec

获取Rspec视图规范以呈现应用程序布局

所以我们在项目中有各种各样的视图规范: https://github.com/tansaku/LocalSupport/blob/master/spec/views/organizations/index.html.erb_spec.rb 但是我们在测试gmap4rails gem生成的javascript时遇到了麻烦。 我想我已经将问题跟踪到了视图规范似乎没有呈现应用程序布局的问题。 我们有一个单独的应用程序布局视图规范,它可以工作,并且可以使应用程序布局正常。 但是,当我们运行视图规范时,应用程序布局代码不会被渲染。 任何帮助都非常感谢

Capybara不等待ajax完成

我正在开发一个由EmberJS前端组成的应用程序,它通过REST与Rails服务器通信。 在我的应用程序中通过向导来创建一个作业,所以我想测试是否创建了该作业,这是我的测试。 feature ‘Processing step three’, :js => true do background do to_step_2 to_step_3 end scenario ‘Creating job’, js: true do within ‘#login’ do fill_in ‘Email’, with: @customer.email fill_in ‘Password’, with: @customer.password click_button ‘Login’ end expect { click_link ‘Create job’ }.to change(Job, :count).by(1) end end 因此,当用户填写所有内容时,最后他们点击创建作业,就像我的测试所描述的那样。 当我手动操作时,这工作正常,但当我运行我的规格时,我得到。 1) Creating a new job Processing step three […]

Rspec控制器错误需要但是使用进行渲染

新的测试,我很难让一些控制器测试通过。 以下控制器测试将引发错误: expecting but rendering with 我在我的一个控制器规格中有以下内容: require ‘spec_helper’ describe NasController do render_views login_user describe “GET #nas” do it “populates an array of devices” do @location = FactoryGirl.create(:location) @location.users << @current_user @nas = FactoryGirl.create(:nas, location_id: @location.id ) get :index assigns(:nas).should eq([@nas]) end it "renders the :index view" do response.should render_template(:index) end end 在我的控制器宏中,我有这个: def login_user […]

使用rspec测试设计视图

我已经生成了运行rails g devise:views Devise的视图,现在想测试它们。 这就是我想出的: require ‘spec_helper’ describe “devise/sessions/new” do before do render end it “renders the form to log in” do rendered.should have_selector(“form”, action: user_session_path, method: :post) do |form| end end end 对于render语句,它给了我undefined local variable or method ‘resource’ 。 谷歌搜索后我发现我应该添加 @user.should_receive(:resource).and_return(User.new) 在render语句之前 – 但它仍然给我相同的错误,我不确定如何使用它。 我究竟做错了什么? 谢谢你的帮助。

Rspec中的test.host测试Rails重定向

这是我在Rails 3.1.12应用程序中的test.rb环境文件中的一行: config.action_mailer.default_url_options = config.action_controller.default_url_options = { :host => “127.0.0.1”, :port => 3000 } 现在这是我做的测试: subject { get :success } subject.should redirect_to(:home) 这会产生错误: Failure/Error: subject.should redirect_to(:home) Expected response to be a redirect to but was a redirect to 我做错了什么? 或者,配置测试主机的位置? 这是spec_helper.rb文件以供完整参考。 # This file is copied to spec/ when you run ‘rails generate rspec:install’ ENV[“RAILS_ENV”] […]

来自Hartl教程的bundle exec rspec spec / requests / static_pages_spec.rb无效

我正在关注Michael Hartl的ruby on rails教程来测试示例应用程序(3.2.1测试驱动开发),但是在输入bundle exec rspec spec/requests/static_pages_spec.rb之后我遇到了以下错误 /home/rahul/.rvm/gems/ruby-2.0.0-p247@railstutorial_rails_4_0/gems/selenium-webdriver-2.0.0/lib/selenium/webdriver/common/zipper.rb:1:in `require’: cannot load such file — zip/zip (LoadError) from /home/rahul/.rvm/gems/ruby-2.0.0-p247@railstutorial_rails_4_0/gems/selenium-webdriver-2.0.0/lib/selenium/webdriver/common/zipper.rb:1:in `’ from /home/rahul/.rvm/gems/ruby-2.0.0-p247@railstutorial_rails_4_0/gems/selenium-webdriver-2.0.0/lib/selenium/webdriver/common.rb:9:in `require’ from /home/rahul/.rvm/gems/ruby-2.0.0-p247@railstutorial_rails_4_0/gems/selenium-webdriver-2.0.0/lib/selenium/webdriver/common.rb:9:in `’ from /home/rahul/.rvm/gems/ruby-2.0.0-p247@railstutorial_rails_4_0/gems/selenium-webdriver-2.0.0/lib/selenium/webdriver.rb:29:in `require’ from /home/rahul/.rvm/gems/ruby-2.0.0-p247@railstutorial_rails_4_0/gems/selenium-webdriver-2.0.0/lib/selenium/webdriver.rb:29:in `’ from /home/rahul/.rvm/gems/ruby-2.0.0-p247@railstutorial_rails_4_0/gems/selenium-webdriver-2.0.0/lib/selenium-webdriver.rb:1:in `require’ from /home/rahul/.rvm/gems/ruby-2.0.0-p247@railstutorial_rails_4_0/gems/selenium-webdriver-2.0.0/lib/selenium-webdriver.rb:1:in `’ from /home/rahul/.rvm/gems/ruby-2.0.0-p247@global/gems/bundler-1.3.5/lib/bundler/runtime.rb:72:in `require’ from /home/rahul/.rvm/gems/ruby-2.0.0-p247@global/gems/bundler-1.3.5/lib/bundler/runtime.rb:72:in `block (2 levels) in require’ from /home/rahul/.rvm/gems/ruby-2.0.0-p247@global/gems/bundler-1.3.5/lib/bundler/runtime.rb:70:in `each’ from /home/rahul/.rvm/gems/ruby-2.0.0-p247@global/gems/bundler-1.3.5/lib/bundler/runtime.rb:70:in `block in require’ […]

我的应用程序访问远程数据库。 如何有效地运行unit testing?

我有一个配置,除了本地postgresql数据库,我的Rails应用程序还访问远程AWS数据库。 我的问题是,即使在不涉及远程数据库的测试中,应用程序每次都建立与远程数据库的连接,因此我的测试运行起来很懒散。 是否有一种干净的方法来禁用对不需要它的rspec测试的远程数据库服务器的访问? (并为那些需要它的测试启用它?) 我能想到的最好的是将我的rspec测试划分为两个独立的部分 – 那些不需要访问远程数据库和那些部分 – 并使用环境变量来相应地启用或禁用部分config / database.yaml 。 为了清楚起见,我的config/database.yaml文件包含(部分): # file: config/database.yaml # Define connections to the external database remote: adapter: mysql database: remote username: password: host: awsserver-production-mysql.abcdef1234567890.us-west-2.rds.amazonaws.com port: 3306 test: adapter: postgresql encoding: unicode database: MyApp_test pool: 5 username: password: (注意:如果您想知道,使用模拟将无济于事:甚至在测试开始运行之前就建立了远程数据库连接。而vcr只拦截HTTP连接 – 数据库连接使用不同的机制。) 更新 我找到了如何动态建立连接的示例: def connect_to_myapp_database ActiveRecord::Base.establish_connection(:adapter => “mysql”, […]

Rspec 3弃用警告:不推荐使用example_group子分区过滤。 请使用subhash直接过滤

运行Rspec版本3测试时,我收到以下弃用警告: 不推荐使用:example_group subhash进行过滤。 请使用subhash直接过滤。 从/path/to/file.rb:6:in`block in’调用。 不推荐使用:example_group subhash进行过滤。 请使用subhash直接过滤。 从/path/to/file.rb:8:in`block in’调用。 从path /到/ file.rb: RSpec.configure do |config| module MyCodeHelpers # end config.include MyCodeHelpers, example_group: { :file_path => %r(spec/services/my_code) } config.before(:all, example_group: { :file_path => %r(spec/services/my_code) }) do @stub = true end end 这只是意味着删除:file_path值周围的’example_group:{}’(见下文)? config.include MyCodeHelpers, :file_path => %r(spec/services/my_code) 和 config.before(:all, :file_path => %r(spec/services/my_code)) do @stub […]

RSpec spec_helper访问会话变量

我是RoR和Rspec的新手。 我正在尝试为家庭控制器编写Rspec测试,并使用登录和注销等测试。我希望能够使用spec_helper.rb中的方法登录/注销用户 但是,当我查看spec_helper.rb时,它说它无法找到会话变量或类似post的方法。 为什么是这样? 如果问题有点含糊,请道歉。 我不太确定如何解决这个问题。 谢谢

针对exception传递的should_receive和should_not_receive的RSpec

我有一个非常奇怪的rspec情况。 我试图测试我的函数是否正确处理exception。 以下是我的代码: 在User.rb中: def welcome_user begin send_welcome_mail(self) rescue Exception => exception ErrorMessage.add(exception, user_id: self.id) end end end 在user_spec.rb中 it “adds to error message if an exception is thrown” do mock_user = User.new mock_user.stub(:send_welcome_mail).and_raise(Exception) ErrorMessage.should_receive(:add) mock_user.welcome_user end 测试通过,但是当我将ErrorMessage.should_receive(:add)更改为ErrorMessage.should_not_receive(:add) ,它还通过了任何见解?