Tag: rspec

将轨道错误发送到rspec输出

我将Capybara与rspec结合使用,用于rails应用程序的集成测试。 我希望在测试期间生成的任何错误(路由错误,控制器中的错误,任何内容)与rspec输出中的“puts”语句相同。 这可能吗? 另外,这是一个合理的想法,还是我只是愚蠢?

如何使用Capybara和RSpec检查会话哈希?

在Stack Overflow上找到关于此的各种post,但没有找到解决方案。 在集成测试中,如何使用Capybara和RSpec检查会话哈希? puts session.inspect undefined method for nil错误抛出一个undefined method for nil 。

attach_file与capybara-webkit的工作方式不正确

我正在尝试将一些文件附加到此输入: 当我使用selenium驱动程序时,我的代码是: attach_file(‘image’, File.absolute_path(‘../pictures/pic1.JPG’)) attach_file(‘image’, File.absolute_path(‘../pictures/pic2.JPG’)) attach_file(‘image’, File.absolute_path(‘../pictures/pic3.JPG’)) 当我使用capybara-webkit时出现问题:由于隐藏了输入并且某些元素与其重叠,我需要更改一些css属性: page.execute_script(“$(‘input[name=image]’).css(‘opacity’,’1′)”) page.execute_script(“$(‘input[name=image]’).css(‘position’,’fixed’)”) page.execute_script(“$(‘input[name=image]’).css(‘top’,’620px’)”) 即使测试通过,图片也不会以正确的方式上传。 如果我使用page.save_screenshot(‘after_upload.png’)来查看发生了什么: 预期结果(使用selenium驱动时的结果) – > 使用capybara-webkit时的实际结果 – >

Rspec确保我们最终走上了正确的道路

有没有办法我们可以测试我们最终在rspec的预期路径? 就像是: it “should be the forgot password path” do response.should redirect_to(new_user_password_path) end 这给了我一个错误: Failure/Error: response.should redirect_to(new_user_password_path) ArgumentError: @request must be an ActionDispatch::Request

Rails和RSpec:在不同的命名空间(模块)中测试具有相同名称的控制器

我有使用RSpec 3.4.0测试的rails 4.1.16 API应用程序,我遇到了在不同模块中测试名为同名的类的问题。 结构是: app/controllers/bar/notifications_controller.rb class Bar::NotificationsController < ApiController … end 和控制器在不同的模块中具有相同的名称: app/controllers/foo/bar/notifications_controller.rb module Foo class Bar::NotificationsController < ApiController … end end Foo是一个新模块,还没有测试。 添加之后,旧的Bar::NotificationsController所有相应控制器测试都开始失败。 规范文件: spec/controllers/bar/notifications_controller_spec.rb require ‘spec_helper’ describe Bar::NotificationsController, type: :controller do … end 该规范文件中的所有测试都失败并出现相同的错误: RuntimeError: @controller is nil: make sure you set it in your test’s setup method. 当我更改Foo模块中的控制器名称时,问题不存在: app/controllers/foo/bar/foo_notifications_controller.rb module Foo […]

每个循环使用Rspec的unit testing用例

我的模型中有以下方法 def get_performance_data para, child_para_hash performance_graph_data = {} child_para_hash.each do |cp| performance_graph_data[cp] = fetch_per_child_para_data(para, cp) end performance_graph_data end def fetch_per_child_para_data para, child_para test_performances.where(network_data_condition(para, child_para)).select(“AVG(value)avg_value, activity”).group(‘act_num’) end 我在理解如何为模型方法中的每个循环编写测试用例时遇到问题。

rspec简单示例在集成测试中获取请求变量的错误

这是一个采用的rails应用程序,没有测试。 我试图在集成测试中测试omniauth但是我收到了一个错误( 编辑我基于此: https : //github.com/intridea/omniauth/wiki/Integration-Testing )。 这反映了我对Rspec缺乏了解。 似乎默认情况下请求对象可用。 我有我的spec / spec_helper.rb: config.include IntegrationSpecHelper, :type => :request Capybara.default_host = ‘http://localhost:3000’ OmniAuth.config.test_mode = true OmniAuth.config.add_mock(:facebook, { :uid => ‘12345’ }) 在我的spec / integration / login_spec中: require ‘spec_helper’ describe ServicesController, “OmniAuth” do before do puts OmniAuth.config.mock_auth[:facebook] puts request # comes back blank request.env[“omniauth.auth”] = OmniAuth.config.mock_auth[:facebook] end it […]

如何在RSpec和Rails中处理模拟嵌套资源?

我有一个用户阅读列表的嵌套资源(一个用户has_many阅读列表)。 我试图模拟我的控制器规格中的所有内容,但无法保持简洁。 这是#show操作的前面代码: @reading_lists = mock(“Reading lists”) @reading_lists.stub!(:find).with(“1”).and_return(@reading_list) @user = mock_model(User, :reading_lists => @reading_lists) User.stub!(:find).with(“1”).and_return(@user) get :show, :user_id => “1”, :id => “1” 正在测试: def show @user = User.find(params[:user_id]) @reading_list = @user.reading_lists.find params[:id] end 这似乎是一个疯狂的样板 – 是否有更好的方法来模拟它?

RSpec和Factory用于测试下一个,prev记录

我有一个User模型,它有下一个和上一个用户的方法: def next_user User.where(“id > ?”, id).order(“id ASC”).first end def prev_user User.where(“id < ?", id).order("id DESC").first end 我正在尝试使用RSpec和Factory girl设置测试来测试这个,并且我不确定这个方法 – 我只是开始使用TDD。 我正在尝试这个: describe “previous_next” do let(:user) { FactoryGirl.create(:user) } let(:next_user) { FactoryGirl.create(:user) } it “should know the next user” do expect(user.next_user).to eq next_user end it “should know the previous user” do expect(next_user.prev_user).to eq user end […]

RSpec未显示正确的默认值

我有一个名为Score的模型的规范,代码如下: # == Schema Information # # Table name: scores # # id :integer not null, primary key # score :integer default(0), not null # exam_id :integer # user_id :integer # questions_answered :integer default(0), not null # created_at :datetime # updated_at :datetime # require ‘rails_helper’ RSpec.describe Score, type: :model do describe “when a new score […]