Tag: 测试

在rails中设置测试环境变量而不放入源代码

我正在使用Twilio作为应用程序并在生产中使用heroku的CLI设置auth令牌。 我正在使用sms-spec ( https://github.com/monfresh/sms-spec )在本地测试我的应用程序的Twilio集成。 我想在测试环境中将ENV[‘TWILIO_AUTH_TOKEN’]为我的令牌。 每当我进行更改时,我都会使用guard来自动运行测试,因此我不希望每次运行测试时都必须手动设置ENV变量。 出于安全原因,我也不想将令牌放在源代码中。 有没有办法可以为我的本地测试环境设置ENV变量,使其成为永久性而不是我的源代码? 我花了几个小时研究这个,似乎无法找到如何做到这一点的好解释。 任何帮助深表感谢 :)

Ruby on Rails方法控制器中的模拟

我正在尝试在我的一个rails控制器(在这种情况下,Instagram.get_access_token)中模拟对外部API的方法调用,我遇到了一些麻烦。 如上所述,代码仍在调用真正的Instagram.get_access_token方法。 我怎么让控制器使用我的简单模拟? sessions_controller.rb: class SessionsController < ApplicationController require 'instagram' include ApplicationHelper def auth_callback response = Instagram.get_access_token(params[:code], redirect_uri: auth_callback_url) # end end sessions_controller_spec.rb: require ‘spec_helper’ require ‘ostruct’ describe SessionsController do describe “GET #auth_callback” do context “when there is an existing user” do let(:response) { OpenStruct.new(access_token: “good_access_token”) } it “parses an access_token from the get response” […]

Rails 4 Minitest测试虚拟属性

所以我有一个类TimeBank,它正在更新,并且正在获得两个新的虚拟属性,它们将操纵另外两个现有属性(DB列)。 class TimeBank < ActiveRecord::Base # validations, consts, few other methods omitted for brevity def date_worked @date_worked ||= self.start.to_date rescue Date.current end def date_worked=(date_worked_val) self.attributes['date_worked'] = date_worked_val end def hours_worked @hours_worked ||= hours rescue NoMethodError nil end def hours_worked=(hours_worked_val) self.attributes['hours_worked'] = hours_worked_val end def hours if attributes["hours"] read_attribute(:hours).to_f else ((finish – start)/1.hour).to_f end rescue NoMethodError […]

具有多态关联的灯具不起作用

我正在尝试实现Rolify gem但是无法添加具有范围的灯具。 下面的(模型)测试的最后一行失败,因为目前主持人角色似乎全局给予@user ,而不是仅给予组织1。 下面的灯具没有使用resource_id和resource_type ,它们在gem文件中提到了夹具,但我不知道如何使用它们。 如何将主持人角色的范围设置为仅限组织1? roles.yml moderator: id: 1 resource: one (Organization) users.yml里 one: email: example@example.com roles: moderator, organizations(:one) # I was hoping this would set the scope of the role to organization one but it isn’t (seems to set the role globally). test.rb def setup @moderator_role = roles(:moderator) @organization1 = organizations(:one) @organization2 […]

从测试驱动开发到哪里开始?

我对测试驱动开发比较陌生,我只是想知道我应该从哪里开始? 我理解如何进行测试。 我只是说我应该先测试什么? 这是最好的做法吗? 我应该先测试模型吗? 控制器? 我应该首先编写集成测试,然后从那里构建所有内容以使其通过吗? 对此有何看法?

rspec测试结果Capybara :: ElementNotFound

我被要求帮助Rails应用程序。 我正在经历一些失败的rpsec测试并让它们通过。 我有一个我撞在墙上的头。 我们正在使用Rails 3.2.13和Ruby 2.0注意:我完全是关于rspec的nube。 我有一个具有兽医师资格的表格。 测试正在寻找这个并没有找到它。 我收到错误: Capybara::ElementNotFound: Unable to find css “form#vet-as-faculty 我在测试中的其他表单元素上也得到了类似的错误,但测试失败或此时失败。 我知道这“通常”意味着表单上的标记不正确或者测试中的某些内容拼写错误(我发现了其中的一些)。 但这一个我无法得到。 我已经用Google搜索了这个错误并发现了一些东西,但似乎没有什么能帮助我解决这个问题。 我希望你们这里的大师会为我看到一些简单的东西。 表格/视图(部分): { :action => :vet_as_faculty }, :html => { :id => “vet-as-faculty” } do |f| %> Vet Me As Faculty Faculty URL 查看源显示为: Vet Me As Faculty Faculty URL … 所以,我认为id为’id =“vet-as-faculty”’。 但是,测试没有看到这一点。 测试代码: context […]

如何使用rspec和factory测试模型的方法

我是铁杆上的新手,我必须用’Rspec’,’shoulda’和’factory girl’gem为现有的rails应用程序编写测试。 我可以测试非特定的测试,例如validates_presence_of:与’sholda’匹配器的东西。 但我想测试模型中的方法。 我可以想象我需要做什么,但我不能写作。 这就是我所说的一个例子: . . . context ‘is editable if project is not started’ do setup do @brief=Factory(:brief) @started_project=Factory(:project_started, :brief => @brief) @brief_have_no_project=Factory(:brief) end specify “with editable brief” do @brief.brand_info = ‘bla bla bla’ #change brand info, this is impossible #i can’t compose this section :S end specify “with non-editable brief” do […]

将测试数据存储在文件中的最佳格式是什么?

在我们的项目中,我们使用存储在.sql文件中的测试数据,我们有时会调用这些文件来使用这些测试数据重新创建数据库。 在这个文件中,我们存储如下数据: Insert into Table1 (‘col1’, ‘col2’) values (‘val1’, ‘val2’); Insert into Table1 (‘col1’, ‘col2’) values (‘val3’, ‘val2’); Insert into Table1 (‘col1’, ‘col2’) values (‘val4’, ‘val2’); … 问题是当我们添加或删除列时 ,我们必须遍历所有文件(在我的情况下,有许多大文件)并手动添加列和相应的值:这是大脑中的巨大痛苦。 所以我在想,其他人如何存储他们的测试数据? 我认为使用.yml文件(键值对)的rails方式是好方法,但是如何在非ruby-rails环境中使用它? 例如在rails中: david: name: David Heinemeier Hansson birthday: 1979-10-15 profession: Systems development steve: name: Steve Ross Kellock birthday: 1974-09-27 profession: guy with keyboard

Rails 4 – 所有测试失败(甚至断言为真)

新的rails并且刚刚开始学习测试。 我们遇到了一个问题,可能有一个简单的答案,让我们难过。 我们已经编写了一些代码并使其在开发中工作(只是静态代码和具有Devise的登录系统)。 我们文件中的唯一测试用例是由rails生成的,例如: test “should get about” do get :about assert_response :success end 但是,当我们运行bundle exec rake test ,令我们惊讶的是,我们每次测试都失败了 。 10 runs, 0 assertions, 0 failures, 10 errors, 0 skips 每个测试都有相同的错误: ActiveRecord::StatementInvalid: PG::NotNullViolation: ERROR: null value in column “email” violates not-null constraint DETAIL: Failing row contains (980190962, null, 2015-10-27 09:40:30, 2015-10-27 09:40:30). : INSERT INTO […]

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

假设我有一个列出文章的页面。 控制器中的代码曾经是 # 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(…) 或者其他的东西。 那么我的请求规范现在应该是什么样的呢? 我不想只是将应用程序代码复制并粘贴到测试中,但同时,条件和顺序现在相当复杂,因此测试所有预期元素的存在和顺序肯定会失败,除非我模仿索引控制器。 最好的方法是什么,避免如此具体地进行测试,复制应用程序代码? 将查询重构为某个中心位置(如模型)并在测试中重复使用它?