Tag: 嘲笑

如何在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版本2.14的未定义方法`rspec_reset’

我正在尝试使用rspec版本2.14执行测试用例,我收到了以下错误 undefined method `rspec_reset’ for 我想在类上使用rspec_reset。 相同的测试用例正在使用rspec 2.13.1。 那么在2.13之后rspec_reset方法是否可用?

来自csv.read模拟文件的rspec测试结果

我正在使用ruby 1.9而我正在尝试做BDD。 我的第一个测试“应该在csv中读取”,但是第二个我需要模拟文件对象的测试却没有。 这是我的型号规格: require ‘spec_helper’ describe Person do describe “Importing data” do let(:person) { Person.new } let(:data) { “title\tsurname\tfirstname\t\rtitle2\tsurname2\tfirstname2\t\r”} let(:result) {[[“title”,”surname”,”firstname”],[“title2″,”surname2″,”firstname2”]] } it “should read in the csv” do CSV.should_receive(:read). with(“filename”, :row_sep => “\r”, :col_sep => “\t”) person.import(“filename”) end it “should have result” do filename = mock(File, :exists? => true, :read => data) person.import(filename).should […]

如何使用Rails和minitest模拟OmniAuth哈希?

我正在使用Rails 5和minitest。 我想模拟登录我的会话控制器,它依赖于omniauth(我使用谷歌和FB登录)。 我在我的控制器test,test / controllers / rates_controller_test.rb中有这个, class RatesControllerTest < ActionDispatch::IntegrationTest # Login the user def setup logged_in_user = users(:one) login_with_user(logged_in_user) end 然后我尝试在我的测试助手test / test_helper.rb中设置登录, class ActiveSupport::TestCase # Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order. fixtures :all def setup_omniauth_mock(user) OmniAuth.config.test_mode = true omniauth_hash = { ‘provider’ => ‘google’, ‘uid’ => […]

摩卡和嵌套对象

如果这是一个愚蠢的问题,我很嘲笑。 我可以用mocha做以下事情: person.expects(:first_name).returns(‘David’) 如何模拟嵌套对象? 假设我有一个属于一个人的产品,我想得到那个人的名字。 在我的应用程序中,我可能会这样做: product.person.first_name 如何使用模拟获得相同的结果?

Test :: Unit如何测试文件操作和文件内容

我正在寻找一种方法来测试使用带有test_unit的File类的方法。 这与Rspec的问题完全相同,但如何使它与test_unit一起使用。 def foo File.open “filename”, “w” do |file| file.write(“text”) end end 测试将是什么: require ‘test/unit’ def test_foo … end 谢谢你的帮助。

Rubyunit testing技术,Mocking和Stubbing

我被招募为SW Dev,我正在尝试使用RSPEC和RR进行unit testing,但是很难决定特定策略,主要是因为我被分配给已经编码的unit testing书面。 考虑以下代码,它是一个名为method1的大方法的一部分: if ([“5234541252”, “6236253223”].include?(self.id)) self.DoCheck logs.add ‘Doing check’, “id = #{self.id}” return end 这部分方法的相关unit testing将是这样的: “should do check only if id = 5234541252 or 6236253223” 但我遇到了几个问题,基本上涉及最佳实践,例如: 如何使用RR和RSPEC检查是否已从“method1”中调用DoCheck? 我尝试过使用dont_allow(Object).DoCheck但是它不起作用。 describe :do_route do it “should do check only if id = 5234541252 or 6236253223” do user = Factory(:User) user.id = “5234541252” dont_allow(user).DoCheck user.method1 […]

结构与测试在ruby中加倍

在结构上使用rspec double有什么优缺点? 例如 before :each do location = double “locatoin” location.stub(:id => 1) end VS before :each do location = Struct.new(“locatoin”, :id) location.new.id = 1 end

如何使用rspec模拟AWS SDK(v2)?

我有一个类使用aws-sdk-rails gem (它是aws-sdk-ruby v2的包装器)从SQS队列读取/处理消息。 如何模拟AWS调用,以便我可以在不调用外部服务的情况下测试我的代码? communicator.rb : class Communicator def consume_messages sqs_client = Aws::SQS::Client.new # consume messages until the queue is empty loop do r = sqs_client.receive_message({ queue_url: “https://sqs.region.amazonaws.com/xxxxxxxxxxxx/foo”, visibility_timeout: 1, max_number_of_messages: 1 }) break if (response.message.length == 0) # process r.messages.first.body r = sqs_client.delete_message({ queue_url: “https://sqs.region.amazonaws.com/xxxxxxxxxxxx/foo”, receipt_handle: r.messages.first.receipt_handle }) end end end

在RSpec测试中嘲笑ActiveRecord关系

我通过测试遇到了这个问题。 我们假设我有两个模型,User和Post,其中用户has_many:posts。 我试图指出一个包含这样的代码块: user = User.find(123) post = user.posts.find(456) 我知道如何模拟User.find和user.posts部分。 user.posts mock返回Post对象的数组。 当它到达.find(456)部分时,所有内容都会在no block givenexception。 所以我的问题是:作为user.posts mock的结果我返回什么,以便.find(456)方法可以使用它? User.first.posts.class说它是Array,但显然有更多的东西使AR风格的查找调用工作。 我对于在返回的对象上模拟find方法的前景并不高兴。 PS在你提出明确而好的答案之前,先停止嘲笑和使用固定装置/用必要的数据为测试数据库播种,这里是catch:legacy scheme。 User和Post都在数据库视图之上工作而不是表,并且更改它以使它们成为测试数据库中的表似乎对我不利。